script_text_editor.cpp 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237
  1. /*************************************************************************/
  2. /* script_text_editor.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2016 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #include "script_text_editor.h"
  30. #include "tools/editor/editor_settings.h"
  31. #include "os/keyboard.h"
  32. #include "tools/editor/script_editor_debugger.h"
  33. #include "tools/editor/editor_node.h"
  34. Vector<String> ScriptTextEditor::get_functions() {
  35. String errortxt;
  36. int line=-1,col;
  37. TextEdit *te=code_editor->get_text_edit();
  38. String text = te->get_text();
  39. List<String> fnc;
  40. if (script->get_language()->validate(text,line,col,errortxt,script->get_path(),&fnc)) {
  41. //if valid rewrite functions to latest
  42. functions.clear();
  43. for (List<String>::Element *E=fnc.front();E;E=E->next()) {
  44. functions.push_back(E->get());
  45. }
  46. }
  47. return functions;
  48. }
  49. void ScriptTextEditor::apply_code() {
  50. if (script.is_null())
  51. return;
  52. // print_line("applying code");
  53. script->set_source_code(code_editor->get_text_edit()->get_text());
  54. script->update_exports();
  55. }
  56. Ref<Script> ScriptTextEditor::get_edited_script() const {
  57. return script;
  58. }
  59. bool ScriptTextEditor::goto_method(const String& p_method) {
  60. Vector<String> functions = get_functions();
  61. String method_search = p_method + ":";
  62. for (int i=0;i<functions.size();i++) {
  63. String function=functions[i];
  64. if (function.begins_with(method_search)) {
  65. int line=function.get_slice(":",1).to_int();
  66. goto_line(line-1);
  67. return true;
  68. }
  69. }
  70. return false;
  71. }
  72. void ScriptTextEditor::_load_theme_settings() {
  73. TextEdit *text_edit = code_editor->get_text_edit();
  74. text_edit->clear_colors();
  75. /* keyword color */
  76. text_edit->set_custom_bg_color(EDITOR_DEF("text_editor/background_color",Color(0,0,0,0)));
  77. text_edit->add_color_override("completion_background_color", EDITOR_DEF("text_editor/completion_background_color", Color(0,0,0,0)));
  78. text_edit->add_color_override("completion_selected_color", EDITOR_DEF("text_editor/completion_selected_color", Color::html("434244")));
  79. text_edit->add_color_override("completion_existing_color", EDITOR_DEF("text_editor/completion_existing_color", Color::html("21dfdfdf")));
  80. text_edit->add_color_override("completion_scroll_color", EDITOR_DEF("text_editor/completion_scroll_color", Color::html("ffffff")));
  81. text_edit->add_color_override("completion_font_color", EDITOR_DEF("text_editor/completion_font_color", Color::html("aaaaaa")));
  82. text_edit->add_color_override("font_color",EDITOR_DEF("text_editor/text_color",Color(0,0,0)));
  83. text_edit->add_color_override("line_number_color",EDITOR_DEF("text_editor/line_number_color",Color(0,0,0)));
  84. text_edit->add_color_override("caret_color",EDITOR_DEF("text_editor/caret_color",Color(0,0,0)));
  85. text_edit->add_color_override("caret_background_color",EDITOR_DEF("text_editor/caret_background_color",Color(0,0,0)));
  86. text_edit->add_color_override("font_selected_color",EDITOR_DEF("text_editor/text_selected_color",Color(1,1,1)));
  87. text_edit->add_color_override("selection_color",EDITOR_DEF("text_editor/selection_color",Color(0.2,0.2,1)));
  88. text_edit->add_color_override("brace_mismatch_color",EDITOR_DEF("text_editor/brace_mismatch_color",Color(1,0.2,0.2)));
  89. text_edit->add_color_override("current_line_color",EDITOR_DEF("text_editor/current_line_color",Color(0.3,0.5,0.8,0.15)));
  90. text_edit->add_color_override("word_highlighted_color",EDITOR_DEF("text_editor/word_highlighted_color",Color(0.8,0.9,0.9,0.15)));
  91. text_edit->add_color_override("number_color",EDITOR_DEF("text_editor/number_color",Color(0.9,0.6,0.0,2)));
  92. text_edit->add_color_override("function_color",EDITOR_DEF("text_editor/function_color",Color(0.4,0.6,0.8)));
  93. text_edit->add_color_override("member_variable_color",EDITOR_DEF("text_editor/member_variable_color",Color(0.9,0.3,0.3)));
  94. text_edit->add_color_override("mark_color", EDITOR_DEF("text_editor/mark_color", Color(1.0,0.4,0.4,0.4)));
  95. text_edit->add_color_override("breakpoint_color", EDITOR_DEF("text_editor/breakpoint_color", Color(0.8,0.8,0.4,0.2)));
  96. text_edit->add_color_override("search_result_color",EDITOR_DEF("text_editor/search_result_color",Color(0.05,0.25,0.05,1)));
  97. text_edit->add_color_override("search_result_border_color",EDITOR_DEF("text_editor/search_result_border_color",Color(0.1,0.45,0.1,1)));
  98. text_edit->add_constant_override("line_spacing", EDITOR_DEF("text_editor/line_spacing",4));
  99. Color keyword_color= EDITOR_DEF("text_editor/keyword_color",Color(0.5,0.0,0.2));
  100. List<String> keywords;
  101. script->get_language()->get_reserved_words(&keywords);
  102. for(List<String>::Element *E=keywords.front();E;E=E->next()) {
  103. text_edit->add_keyword_color(E->get(),keyword_color);
  104. }
  105. //colorize core types
  106. Color basetype_color= EDITOR_DEF("text_editor/base_type_color",Color(0.3,0.3,0.0));
  107. text_edit->add_keyword_color("Vector2",basetype_color);
  108. text_edit->add_keyword_color("Vector3",basetype_color);
  109. text_edit->add_keyword_color("Plane",basetype_color);
  110. text_edit->add_keyword_color("Quat",basetype_color);
  111. text_edit->add_keyword_color("AABB",basetype_color);
  112. text_edit->add_keyword_color("Matrix3",basetype_color);
  113. text_edit->add_keyword_color("Transform",basetype_color);
  114. text_edit->add_keyword_color("Color",basetype_color);
  115. text_edit->add_keyword_color("Image",basetype_color);
  116. text_edit->add_keyword_color("InputEvent",basetype_color);
  117. text_edit->add_keyword_color("Rect2",basetype_color);
  118. text_edit->add_keyword_color("NodePath",basetype_color);
  119. //colorize engine types
  120. Color type_color= EDITOR_DEF("text_editor/engine_type_color",Color(0.0,0.2,0.4));
  121. List<StringName> types;
  122. ObjectTypeDB::get_type_list(&types);
  123. for(List<StringName>::Element *E=types.front();E;E=E->next()) {
  124. String n = E->get();
  125. if (n.begins_with("_"))
  126. n = n.substr(1, n.length());
  127. text_edit->add_keyword_color(n,type_color);
  128. }
  129. //colorize comments
  130. Color comment_color = EDITOR_DEF("text_editor/comment_color",Color::hex(0x797e7eff));
  131. List<String> comments;
  132. script->get_language()->get_comment_delimiters(&comments);
  133. for(List<String>::Element *E=comments.front();E;E=E->next()) {
  134. String comment = E->get();
  135. String beg = comment.get_slice(" ",0);
  136. String end = comment.get_slice_count(" ")>1?comment.get_slice(" ",1):String();
  137. text_edit->add_color_region(beg,end,comment_color,end=="");
  138. }
  139. //colorize strings
  140. Color string_color = EDITOR_DEF("text_editor/string_color",Color::hex(0x6b6f00ff));
  141. List<String> strings;
  142. script->get_language()->get_string_delimiters(&strings);
  143. for (List<String>::Element *E=strings.front();E;E=E->next()) {
  144. String string = E->get();
  145. String beg = string.get_slice(" ",0);
  146. String end = string.get_slice_count(" ")>1?string.get_slice(" ",1):String();
  147. text_edit->add_color_region(beg,end,string_color,end=="");
  148. }
  149. //colorize symbols
  150. Color symbol_color= EDITOR_DEF("text_editor/symbol_color",Color::hex(0x005291ff));
  151. text_edit->set_symbol_color(symbol_color);
  152. }
  153. void ScriptTextEditor::reload_text() {
  154. ERR_FAIL_COND(script.is_null()) ;
  155. TextEdit *te = code_editor->get_text_edit();
  156. int column = te->cursor_get_column();
  157. int row = te->cursor_get_line();
  158. int h = te->get_h_scroll();
  159. int v = te->get_v_scroll();
  160. te->set_text(script->get_source_code());
  161. te->clear_undo_history();
  162. te->cursor_set_line(row);
  163. te->cursor_set_column(column);
  164. te->set_h_scroll(h);
  165. te->set_v_scroll(v);
  166. te->tag_saved_version();
  167. code_editor->update_line_and_column();
  168. }
  169. void ScriptTextEditor::_notification(int p_what) {
  170. if (p_what==NOTIFICATION_READY) {
  171. //emit_signal("name_changed");
  172. }
  173. }
  174. void ScriptTextEditor::add_callback(const String& p_function,StringArray p_args) {
  175. String code = code_editor->get_text_edit()->get_text();
  176. int pos = script->get_language()->find_function(p_function,code);
  177. if (pos==-1) {
  178. //does not exist
  179. code_editor->get_text_edit()->deselect();
  180. pos=code_editor->get_text_edit()->get_line_count()+2;
  181. String func = script->get_language()->make_function("",p_function,p_args);
  182. //code=code+func;
  183. code_editor->get_text_edit()->cursor_set_line(pos+1);
  184. code_editor->get_text_edit()->cursor_set_column(1000000); //none shall be that big
  185. code_editor->get_text_edit()->insert_text_at_cursor("\n\n"+func);
  186. }
  187. code_editor->get_text_edit()->cursor_set_line(pos);
  188. code_editor->get_text_edit()->cursor_set_column(1);
  189. }
  190. void ScriptTextEditor::update_settings() {
  191. code_editor->get_text_edit()->set_auto_brace_completion(EditorSettings::get_singleton()->get("text_editor/auto_brace_complete"));
  192. code_editor->get_text_edit()->set_scroll_pass_end_of_file(EditorSettings::get_singleton()->get("text_editor/scroll_past_end_of_file"));
  193. code_editor->get_text_edit()->set_tab_size(EditorSettings::get_singleton()->get("text_editor/tab_size"));
  194. code_editor->get_text_edit()->set_draw_tabs(EditorSettings::get_singleton()->get("text_editor/draw_tabs"));
  195. code_editor->get_text_edit()->set_show_line_numbers(EditorSettings::get_singleton()->get("text_editor/show_line_numbers"));
  196. code_editor->get_text_edit()->set_syntax_coloring(EditorSettings::get_singleton()->get("text_editor/syntax_highlighting"));
  197. code_editor->get_text_edit()->set_highlight_all_occurrences(EditorSettings::get_singleton()->get("text_editor/highlight_all_occurrences"));
  198. code_editor->get_text_edit()->cursor_set_blink_enabled(EditorSettings::get_singleton()->get("text_editor/caret_blink"));
  199. code_editor->get_text_edit()->cursor_set_blink_speed(EditorSettings::get_singleton()->get("text_editor/caret_blink_speed"));
  200. code_editor->get_text_edit()->set_draw_breakpoint_gutter(EditorSettings::get_singleton()->get("text_editor/show_breakpoint_gutter"));
  201. code_editor->get_text_edit()->cursor_set_block_mode(EditorSettings::get_singleton()->get("text_editor/block_caret"));
  202. }
  203. bool ScriptTextEditor::is_unsaved() {
  204. return code_editor->get_text_edit()->get_version()!=code_editor->get_text_edit()->get_saved_version();
  205. }
  206. Variant ScriptTextEditor::get_edit_state() {
  207. Dictionary state;
  208. state["scroll_pos"]=code_editor->get_text_edit()->get_v_scroll();
  209. state["column"]=code_editor->get_text_edit()->cursor_get_column();
  210. state["row"]=code_editor->get_text_edit()->cursor_get_line();
  211. return state;
  212. }
  213. void ScriptTextEditor::trim_trailing_whitespace() {
  214. TextEdit *tx = code_editor->get_text_edit();
  215. bool trimed_whitespace = false;
  216. for (int i = 0; i < tx->get_line_count(); i++) {
  217. String line = tx->get_line(i);
  218. if (line.ends_with(" ") || line.ends_with("\t")) {
  219. if (!trimed_whitespace) {
  220. tx->begin_complex_operation();
  221. trimed_whitespace = true;
  222. }
  223. int end = 0;
  224. for (int j = line.length() - 1; j > -1; j--) {
  225. if (line[j] != ' ' && line[j] != '\t') {
  226. end = j+1;
  227. break;
  228. }
  229. }
  230. tx->set_line(i, line.substr(0, end));
  231. }
  232. }
  233. if (trimed_whitespace) {
  234. tx->end_complex_operation();
  235. tx->update();
  236. }
  237. }
  238. void ScriptTextEditor::tag_saved_version() {
  239. code_editor->get_text_edit()->tag_saved_version();
  240. }
  241. void ScriptTextEditor::goto_line(int p_line, bool p_with_error) {
  242. code_editor->get_text_edit()->cursor_set_line(p_line);
  243. }
  244. void ScriptTextEditor::ensure_focus() {
  245. code_editor->get_text_edit()->grab_focus();
  246. }
  247. void ScriptTextEditor::set_edit_state(const Variant& p_state) {
  248. Dictionary state=p_state;
  249. code_editor->get_text_edit()->set_v_scroll(state["scroll_pos"]);
  250. code_editor->get_text_edit()->cursor_set_column( state["column"]);
  251. code_editor->get_text_edit()->cursor_set_line( state["row"] );
  252. code_editor->get_text_edit()->grab_focus();
  253. //int scroll_pos;
  254. //int cursor_column;
  255. //int cursor_row;
  256. }
  257. String ScriptTextEditor::get_name() {
  258. String name;
  259. if (script->get_path().find("local://")==-1 && script->get_path().find("::")==-1) {
  260. name=script->get_path().get_file();
  261. if (is_unsaved()) {
  262. name+="(*)";
  263. }
  264. } else if (script->get_name()!="")
  265. name=script->get_name();
  266. else
  267. name=script->get_type()+"("+itos(script->get_instance_ID())+")";
  268. return name;
  269. }
  270. Ref<Texture> ScriptTextEditor::get_icon() {
  271. if (get_parent_control() && get_parent_control()->has_icon(script->get_type(),"EditorIcons")) {
  272. return get_parent_control()->get_icon(script->get_type(),"EditorIcons");
  273. }
  274. return Ref<Texture>();
  275. }
  276. void ScriptTextEditor::set_edited_script(const Ref<Script>& p_script) {
  277. ERR_FAIL_COND(!script.is_null());
  278. script=p_script;
  279. _load_theme_settings();
  280. code_editor->get_text_edit()->set_text(script->get_source_code());
  281. code_editor->get_text_edit()->clear_undo_history();
  282. code_editor->get_text_edit()->tag_saved_version();
  283. emit_signal("name_changed");
  284. code_editor->update_line_and_column();
  285. }
  286. void ScriptTextEditor::_validate_script() {
  287. String errortxt;
  288. int line=-1,col;
  289. TextEdit *te=code_editor->get_text_edit();
  290. String text = te->get_text();
  291. List<String> fnc;
  292. if (!script->get_language()->validate(text,line,col,errortxt,script->get_path(),&fnc)) {
  293. String error_text="error("+itos(line)+","+itos(col)+"): "+errortxt;
  294. code_editor->set_error(error_text);
  295. } else {
  296. code_editor->set_error("");
  297. line=-1;
  298. if (!script->is_tool()) {
  299. script->set_source_code(text);
  300. script->update_exports();
  301. //script->reload(); //will update all the variables in property editors
  302. }
  303. functions.clear();
  304. for (List<String>::Element *E=fnc.front();E;E=E->next()) {
  305. functions.push_back(E->get());
  306. }
  307. }
  308. line--;
  309. for(int i=0;i<te->get_line_count();i++) {
  310. te->set_line_as_marked(i,line==i);
  311. }
  312. emit_signal("name_changed");
  313. }
  314. static Node* _find_node_for_script(Node* p_base, Node*p_current, const Ref<Script>& p_script) {
  315. if (p_current->get_owner()!=p_base && p_base!=p_current)
  316. return NULL;
  317. Ref<Script> c = p_current->get_script();
  318. if (c==p_script)
  319. return p_current;
  320. for(int i=0;i<p_current->get_child_count();i++) {
  321. Node *found = _find_node_for_script(p_base,p_current->get_child(i),p_script);
  322. if (found)
  323. return found;
  324. }
  325. return NULL;
  326. }
  327. static void _find_changed_scripts_for_external_editor(Node* p_base, Node*p_current, Set<Ref<Script> > &r_scripts) {
  328. if (p_current->get_owner()!=p_base && p_base!=p_current)
  329. return;
  330. Ref<Script> c = p_current->get_script();
  331. if (c.is_valid())
  332. r_scripts.insert(c);
  333. for(int i=0;i<p_current->get_child_count();i++) {
  334. _find_changed_scripts_for_external_editor(p_base,p_current->get_child(i),r_scripts);
  335. }
  336. }
  337. void ScriptEditor::_update_modified_scripts_for_external_editor(Ref<Script> p_for_script) {
  338. if (!bool(EditorSettings::get_singleton()->get("external_editor/use_external_editor")))
  339. return;
  340. Set<Ref<Script> > scripts;
  341. Node *base = get_tree()->get_edited_scene_root();
  342. if (base) {
  343. _find_changed_scripts_for_external_editor(base,base,scripts);
  344. }
  345. for (Set<Ref<Script> >::Element *E=scripts.front();E;E=E->next()) {
  346. Ref<Script> script = E->get();
  347. if (p_for_script.is_valid() && p_for_script!=script)
  348. continue;
  349. if (script->get_path()=="" || script->get_path().find("local://")!=-1 || script->get_path().find("::")!=-1) {
  350. continue; //internal script, who cares, though weird
  351. }
  352. uint64_t last_date = script->get_last_modified_time();
  353. uint64_t date = FileAccess::get_modified_time(script->get_path());
  354. if (last_date!=date) {
  355. Ref<Script> rel_script = ResourceLoader::load(script->get_path(),script->get_type(),true);
  356. ERR_CONTINUE(!rel_script.is_valid());
  357. script->set_source_code( rel_script->get_source_code() );
  358. script->set_last_modified_time( rel_script->get_last_modified_time() );
  359. script->update_exports();
  360. }
  361. }
  362. }
  363. void ScriptTextEditor::_code_complete_scripts(void* p_ud,const String& p_code, List<String>* r_options) {
  364. ScriptTextEditor *ste = (ScriptTextEditor *)p_ud;
  365. ste->_code_complete_script(p_code,r_options);
  366. }
  367. void ScriptTextEditor::_code_complete_script(const String& p_code, List<String>* r_options) {
  368. Node *base = get_tree()->get_edited_scene_root();
  369. if (base) {
  370. base = _find_node_for_script(base,base,script);
  371. }
  372. String hint;
  373. Error err = script->get_language()->complete_code(p_code,script->get_path().get_base_dir(),base,r_options,hint);
  374. if (hint!="") {
  375. code_editor->get_text_edit()->set_code_hint(hint);
  376. }
  377. }
  378. void ScriptTextEditor::_breakpoint_toggled(int p_row) {
  379. 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));
  380. }
  381. static void swap_lines(TextEdit *tx, int line1, int line2)
  382. {
  383. String tmp = tx->get_line(line1);
  384. String tmp2 = tx->get_line(line2);
  385. tx->set_line(line2, tmp);
  386. tx->set_line(line1, tmp2);
  387. tx->cursor_set_line(line2);
  388. }
  389. void ScriptTextEditor::_edit_option(int p_op) {
  390. switch(p_op) {
  391. case EDIT_UNDO: {
  392. code_editor->get_text_edit()->undo();
  393. code_editor->get_text_edit()->call_deferred("grab_focus");
  394. } break;
  395. case EDIT_REDO: {
  396. code_editor->get_text_edit()->redo();
  397. code_editor->get_text_edit()->call_deferred("grab_focus");
  398. } break;
  399. case EDIT_CUT: {
  400. code_editor->get_text_edit()->cut();
  401. code_editor->get_text_edit()->call_deferred("grab_focus");
  402. } break;
  403. case EDIT_COPY: {
  404. code_editor->get_text_edit()->copy();
  405. code_editor->get_text_edit()->call_deferred("grab_focus");
  406. } break;
  407. case EDIT_PASTE: {
  408. code_editor->get_text_edit()->paste();
  409. code_editor->get_text_edit()->call_deferred("grab_focus");
  410. } break;
  411. case EDIT_SELECT_ALL: {
  412. code_editor->get_text_edit()->select_all();
  413. code_editor->get_text_edit()->call_deferred("grab_focus");
  414. } break;
  415. case EDIT_MOVE_LINE_UP: {
  416. TextEdit *tx = code_editor->get_text_edit();
  417. Ref<Script> scr = script;
  418. if (scr.is_null())
  419. return;
  420. tx->begin_complex_operation();
  421. if (tx->is_selection_active())
  422. {
  423. int from_line = tx->get_selection_from_line();
  424. int from_col = tx->get_selection_from_column();
  425. int to_line = tx->get_selection_to_line();
  426. int to_column = tx->get_selection_to_column();
  427. for (int i = from_line; i <= to_line; i++)
  428. {
  429. int line_id = i;
  430. int next_id = i - 1;
  431. if (line_id == 0 || next_id < 0)
  432. return;
  433. swap_lines(tx, line_id, next_id);
  434. }
  435. int from_line_up = from_line > 0 ? from_line-1 : from_line;
  436. int to_line_up = to_line > 0 ? to_line-1 : to_line;
  437. tx->select(from_line_up, from_col, to_line_up, to_column);
  438. }
  439. else
  440. {
  441. int line_id = tx->cursor_get_line();
  442. int next_id = line_id - 1;
  443. if (line_id == 0 || next_id < 0)
  444. return;
  445. swap_lines(tx, line_id, next_id);
  446. }
  447. tx->end_complex_operation();
  448. tx->update();
  449. } break;
  450. case EDIT_MOVE_LINE_DOWN: {
  451. TextEdit *tx = code_editor->get_text_edit();
  452. Ref<Script> scr = get_edited_script();
  453. if (scr.is_null())
  454. return;
  455. tx->begin_complex_operation();
  456. if (tx->is_selection_active())
  457. {
  458. int from_line = tx->get_selection_from_line();
  459. int from_col = tx->get_selection_from_column();
  460. int to_line = tx->get_selection_to_line();
  461. int to_column = tx->get_selection_to_column();
  462. for (int i = to_line; i >= from_line; i--)
  463. {
  464. int line_id = i;
  465. int next_id = i + 1;
  466. if (line_id == tx->get_line_count()-1 || next_id > tx->get_line_count())
  467. return;
  468. swap_lines(tx, line_id, next_id);
  469. }
  470. int from_line_down = from_line < tx->get_line_count() ? from_line+1 : from_line;
  471. int to_line_down = to_line < tx->get_line_count() ? to_line+1 : to_line;
  472. tx->select(from_line_down, from_col, to_line_down, to_column);
  473. }
  474. else
  475. {
  476. int line_id = tx->cursor_get_line();
  477. int next_id = line_id + 1;
  478. if (line_id == tx->get_line_count()-1 || next_id > tx->get_line_count())
  479. return;
  480. swap_lines(tx, line_id, next_id);
  481. }
  482. tx->end_complex_operation();
  483. tx->update();
  484. } break;
  485. case EDIT_INDENT_LEFT: {
  486. TextEdit *tx = code_editor->get_text_edit();
  487. Ref<Script> scr = get_edited_script();
  488. if (scr.is_null())
  489. return;
  490. tx->begin_complex_operation();
  491. if (tx->is_selection_active())
  492. {
  493. tx->indent_selection_left();
  494. }
  495. else
  496. {
  497. int begin = tx->cursor_get_line();
  498. String line_text = tx->get_line(begin);
  499. // begins with tab
  500. if (line_text.begins_with("\t"))
  501. {
  502. line_text = line_text.substr(1, line_text.length());
  503. tx->set_line(begin, line_text);
  504. }
  505. // begins with 4 spaces
  506. else if (line_text.begins_with(" "))
  507. {
  508. line_text = line_text.substr(4, line_text.length());
  509. tx->set_line(begin, line_text);
  510. }
  511. }
  512. tx->end_complex_operation();
  513. tx->update();
  514. //tx->deselect();
  515. } break;
  516. case EDIT_INDENT_RIGHT: {
  517. TextEdit *tx = code_editor->get_text_edit();
  518. Ref<Script> scr = get_edited_script();
  519. if (scr.is_null())
  520. return;
  521. tx->begin_complex_operation();
  522. if (tx->is_selection_active())
  523. {
  524. tx->indent_selection_right();
  525. }
  526. else
  527. {
  528. int begin = tx->cursor_get_line();
  529. String line_text = tx->get_line(begin);
  530. line_text = '\t' + line_text;
  531. tx->set_line(begin, line_text);
  532. }
  533. tx->end_complex_operation();
  534. tx->update();
  535. //tx->deselect();
  536. } break;
  537. case EDIT_CLONE_DOWN: {
  538. TextEdit *tx = code_editor->get_text_edit();
  539. Ref<Script> scr = get_edited_script();
  540. if (scr.is_null())
  541. return;
  542. int from_line = tx->cursor_get_line();
  543. int to_line = tx->cursor_get_line();
  544. int column = tx->cursor_get_column();
  545. if (tx->is_selection_active()) {
  546. from_line = tx->get_selection_from_line();
  547. to_line = tx->get_selection_to_line();
  548. column = tx->cursor_get_column();
  549. }
  550. int next_line = to_line + 1;
  551. tx->begin_complex_operation();
  552. for (int i = from_line; i <= to_line; i++) {
  553. if (i >= tx->get_line_count() - 1) {
  554. tx->set_line(i, tx->get_line(i) + "\n");
  555. }
  556. String line_clone = tx->get_line(i);
  557. tx->insert_at(line_clone, next_line);
  558. next_line++;
  559. }
  560. tx->cursor_set_column(column);
  561. if (tx->is_selection_active()) {
  562. tx->select(to_line + 1, tx->get_selection_from_column(), next_line - 1, tx->get_selection_to_column());
  563. }
  564. tx->end_complex_operation();
  565. tx->update();
  566. } break;
  567. case EDIT_TOGGLE_COMMENT: {
  568. TextEdit *tx = code_editor->get_text_edit();
  569. Ref<Script> scr = get_edited_script();
  570. if (scr.is_null())
  571. return;
  572. tx->begin_complex_operation();
  573. if (tx->is_selection_active())
  574. {
  575. int begin = tx->get_selection_from_line();
  576. int end = tx->get_selection_to_line();
  577. // End of selection ends on the first column of the last line, ignore it.
  578. if(tx->get_selection_to_column() == 0)
  579. end -= 1;
  580. for (int i = begin; i <= end; i++)
  581. {
  582. String line_text = tx->get_line(i);
  583. if (line_text.begins_with("#"))
  584. line_text = line_text.substr(1, line_text.length());
  585. else
  586. line_text = "#" + line_text;
  587. tx->set_line(i, line_text);
  588. }
  589. }
  590. else
  591. {
  592. int begin = tx->cursor_get_line();
  593. String line_text = tx->get_line(begin);
  594. if (line_text.begins_with("#"))
  595. line_text = line_text.substr(1, line_text.length());
  596. else
  597. line_text = "#" + line_text;
  598. tx->set_line(begin, line_text);
  599. }
  600. tx->end_complex_operation();
  601. tx->update();
  602. //tx->deselect();
  603. } break;
  604. case EDIT_COMPLETE: {
  605. code_editor->get_text_edit()->query_code_comple();
  606. } break;
  607. case EDIT_AUTO_INDENT: {
  608. TextEdit *te = code_editor->get_text_edit();
  609. String text = te->get_text();
  610. Ref<Script> scr = get_edited_script();
  611. if (scr.is_null())
  612. return;
  613. int begin,end;
  614. if (te->is_selection_active()) {
  615. begin=te->get_selection_from_line();
  616. end=te->get_selection_to_line();
  617. } else {
  618. begin=0;
  619. end=te->get_line_count()-1;
  620. }
  621. scr->get_language()->auto_indent_code(text,begin,end);
  622. te->set_text(text);
  623. } break;
  624. case EDIT_TRIM_TRAILING_WHITESAPCE: {
  625. trim_trailing_whitespace();
  626. } break;
  627. case SEARCH_FIND: {
  628. code_editor->get_find_replace_bar()->popup_search();
  629. } break;
  630. case SEARCH_FIND_NEXT: {
  631. code_editor->get_find_replace_bar()->search_next();
  632. } break;
  633. case SEARCH_FIND_PREV: {
  634. code_editor->get_find_replace_bar()->search_prev();
  635. } break;
  636. case SEARCH_REPLACE: {
  637. code_editor->get_find_replace_bar()->popup_replace();
  638. } break;
  639. case SEARCH_LOCATE_FUNCTION: {
  640. quick_open->popup(get_functions());
  641. } break;
  642. case SEARCH_GOTO_LINE: {
  643. goto_line_dialog->popup_find_line(code_editor->get_text_edit());
  644. } break;
  645. case DEBUG_TOGGLE_BREAKPOINT: {
  646. int line=code_editor->get_text_edit()->cursor_get_line();
  647. bool dobreak = !code_editor->get_text_edit()->is_line_set_as_breakpoint(line);
  648. code_editor->get_text_edit()->set_line_as_breakpoint(line,dobreak);
  649. ScriptEditor::get_singleton()->get_debugger()->set_breakpoint(get_edited_script()->get_path(),line+1,dobreak);
  650. } break;
  651. case DEBUG_REMOVE_ALL_BREAKPOINTS: {
  652. List<int> bpoints;
  653. code_editor->get_text_edit()->get_breakpoints(&bpoints);
  654. for(List<int>::Element *E=bpoints.front();E;E=E->next()) {
  655. int line = E->get();
  656. bool dobreak = !code_editor->get_text_edit()->is_line_set_as_breakpoint(line);
  657. code_editor->get_text_edit()->set_line_as_breakpoint(line,dobreak);
  658. ScriptEditor::get_singleton()->get_debugger()->set_breakpoint(get_edited_script()->get_path(),line+1,dobreak);
  659. }
  660. }
  661. case DEBUG_GOTO_NEXT_BREAKPOINT: {
  662. List<int> bpoints;
  663. code_editor->get_text_edit()->get_breakpoints(&bpoints);
  664. if (bpoints.size() <= 0) {
  665. return;
  666. }
  667. int line=code_editor->get_text_edit()->cursor_get_line();
  668. // wrap around
  669. if (line >= bpoints[bpoints.size() - 1]) {
  670. code_editor->get_text_edit()->cursor_set_line(bpoints[0]);
  671. } else {
  672. for(List<int>::Element *E=bpoints.front();E;E=E->next()) {
  673. int bline = E->get();
  674. if (bline > line) {
  675. code_editor->get_text_edit()->cursor_set_line(bline);
  676. return;
  677. }
  678. }
  679. }
  680. } break;
  681. case DEBUG_GOTO_PREV_BREAKPOINT: {
  682. List<int> bpoints;
  683. code_editor->get_text_edit()->get_breakpoints(&bpoints);
  684. if (bpoints.size() <= 0) {
  685. return;
  686. }
  687. int line=code_editor->get_text_edit()->cursor_get_line();
  688. // wrap around
  689. if (line <= bpoints[0]) {
  690. code_editor->get_text_edit()->cursor_set_line(bpoints[bpoints.size() - 1]);
  691. } else {
  692. for(List<int>::Element *E=bpoints.back();E;E=E->prev()) {
  693. int bline = E->get();
  694. if (bline < line) {
  695. code_editor->get_text_edit()->cursor_set_line(bline);
  696. return;
  697. }
  698. }
  699. }
  700. } break;
  701. case HELP_CONTEXTUAL: {
  702. String text = code_editor->get_text_edit()->get_selection_text();
  703. if (text == "")
  704. text = code_editor->get_text_edit()->get_word_under_cursor();
  705. if (text != "") {
  706. emit_signal("request_help_search",text);
  707. }
  708. } break;
  709. }
  710. }
  711. void ScriptTextEditor::_bind_methods() {
  712. ObjectTypeDB::bind_method("_validate_script",&ScriptTextEditor::_validate_script);
  713. ObjectTypeDB::bind_method("_load_theme_settings",&ScriptTextEditor::_load_theme_settings);
  714. ObjectTypeDB::bind_method("_breakpoint_toggled",&ScriptTextEditor::_breakpoint_toggled);
  715. ObjectTypeDB::bind_method("_edit_option",&ScriptTextEditor::_edit_option);
  716. ObjectTypeDB::bind_method("_goto_line",&ScriptTextEditor::_goto_line);
  717. ObjectTypeDB::bind_method("get_drag_data_fw",&ScriptTextEditor::get_drag_data_fw);
  718. ObjectTypeDB::bind_method("can_drop_data_fw",&ScriptTextEditor::can_drop_data_fw);
  719. ObjectTypeDB::bind_method("drop_data_fw",&ScriptTextEditor::drop_data_fw);
  720. ADD_SIGNAL(MethodInfo("name_changed"));
  721. ADD_SIGNAL(MethodInfo("request_help_search",PropertyInfo(Variant::STRING,"topic")));
  722. }
  723. Control *ScriptTextEditor::get_edit_menu() {
  724. return edit_hb;
  725. }
  726. void ScriptTextEditor::reload(bool p_soft) {
  727. TextEdit *te = code_editor->get_text_edit();
  728. Ref<Script> scr = get_edited_script();
  729. if (scr.is_null())
  730. return;
  731. scr->set_source_code(te->get_text());
  732. bool soft = p_soft || scr->get_instance_base_type()=="EditorPlugin"; //always soft-reload editor plugins
  733. scr->get_language()->reload_tool_script(scr,soft);
  734. }
  735. void ScriptTextEditor::get_breakpoints(List<int> *p_breakpoints) {
  736. code_editor->get_text_edit()->get_breakpoints(p_breakpoints);
  737. }
  738. void ScriptTextEditor::set_tooltip_request_func(String p_method,Object* p_obj) {
  739. code_editor->get_text_edit()->set_tooltip_request_func(p_obj,p_method,this);
  740. }
  741. void ScriptTextEditor::set_debugger_active(bool p_active) {
  742. }
  743. Variant ScriptTextEditor::get_drag_data_fw(const Point2& p_point,Control* p_from) {
  744. return Variant();
  745. }
  746. bool ScriptTextEditor::can_drop_data_fw(const Point2& p_point,const Variant& p_data,Control* p_from) const{
  747. Dictionary d = p_data;
  748. if (d.has("type") &&
  749. (
  750. String(d["type"])=="resource" ||
  751. String(d["type"])=="files" ||
  752. String(d["type"])=="nodes"
  753. ) ) {
  754. return true;
  755. }
  756. return false;
  757. }
  758. #ifdef TOOLS_ENABLED
  759. static Node* _find_script_node(Node* p_edited_scene,Node* p_current_node,const Ref<Script> &script) {
  760. if (p_edited_scene!=p_current_node && p_current_node->get_owner()!=p_edited_scene)
  761. return NULL;
  762. Ref<Script> scr = p_current_node->get_script();
  763. if (scr.is_valid() && scr==script)
  764. return p_current_node;
  765. for(int i=0;i<p_current_node->get_child_count();i++) {
  766. Node *n = _find_script_node(p_edited_scene,p_current_node->get_child(i),script);
  767. if (n)
  768. return n;
  769. }
  770. return NULL;
  771. }
  772. #else
  773. static Node* _find_script_node(Node* p_edited_scene,Node* p_current_node,const Ref<Script> &script) {
  774. return NULL;
  775. }
  776. #endif
  777. void ScriptTextEditor::drop_data_fw(const Point2& p_point,const Variant& p_data,Control* p_from){
  778. Dictionary d = p_data;
  779. if (d.has("type") && String(d["type"])=="resource") {
  780. Ref<Resource> res = d["resource"];
  781. if (!res.is_valid()) {
  782. return;
  783. }
  784. if (res->get_path().is_resource_file()) {
  785. EditorNode::get_singleton()->show_warning("Only resources from filesystem can be dropped.");
  786. return;
  787. }
  788. code_editor->get_text_edit()->insert_text_at_cursor(res->get_path());
  789. }
  790. if (d.has("type") && String(d["type"])=="files") {
  791. Array files = d["files"];
  792. String text_to_drop;
  793. for(int i=0;i<files.size();i++) {
  794. if (i>0)
  795. text_to_drop+=",";
  796. text_to_drop+="\""+String(files[i]).c_escape()+"\"";
  797. }
  798. code_editor->get_text_edit()->insert_text_at_cursor(text_to_drop);
  799. }
  800. if (d.has("type") && String(d["type"])=="nodes") {
  801. Node* sn = _find_script_node(get_tree()->get_edited_scene_root(),get_tree()->get_edited_scene_root(),script);
  802. if (!sn) {
  803. EditorNode::get_singleton()->show_warning("Can't drop nodes because script '"+get_name()+"' is not used in this scene.");
  804. return;
  805. }
  806. Array nodes = d["nodes"];
  807. String text_to_drop;
  808. for(int i=0;i<nodes.size();i++) {
  809. if (i>0)
  810. text_to_drop+=",";
  811. NodePath np = nodes[i];
  812. Node *node = get_node(np);
  813. if (!node) {
  814. continue;
  815. }
  816. String path = sn->get_path_to(node);
  817. text_to_drop+="\""+path.c_escape()+"\"";
  818. }
  819. code_editor->get_text_edit()->insert_text_at_cursor(text_to_drop);
  820. }
  821. }
  822. ScriptTextEditor::ScriptTextEditor() {
  823. code_editor = memnew( CodeTextEditor );
  824. add_child(code_editor);
  825. code_editor->set_area_as_parent_rect();
  826. code_editor->connect("validate_script",this,"_validate_script");
  827. code_editor->connect("load_theme_settings",this,"_load_theme_settings");
  828. code_editor->set_code_complete_func(_code_complete_scripts,this);
  829. code_editor->get_text_edit()->connect("breakpoint_toggled", this, "_breakpoint_toggled");
  830. code_editor->get_text_edit()->set_scroll_pass_end_of_file(EditorSettings::get_singleton()->get("text_editor/scroll_past_end_of_file"));
  831. code_editor->get_text_edit()->set_auto_brace_completion(EditorSettings::get_singleton()->get("text_editor/auto_brace_complete"));
  832. code_editor->get_text_edit()->set_tab_size(EditorSettings::get_singleton()->get("text_editor/tab_size"));
  833. code_editor->get_text_edit()->set_draw_tabs(EditorSettings::get_singleton()->get("text_editor/draw_tabs"));
  834. code_editor->get_text_edit()->set_show_line_numbers(EditorSettings::get_singleton()->get("text_editor/show_line_numbers"));
  835. code_editor->get_text_edit()->set_syntax_coloring(EditorSettings::get_singleton()->get("text_editor/syntax_highlighting"));
  836. code_editor->get_text_edit()->set_highlight_all_occurrences(EditorSettings::get_singleton()->get("text_editor/highlight_all_occurrences"));
  837. code_editor->get_text_edit()->cursor_set_blink_enabled(EditorSettings::get_singleton()->get("text_editor/caret_blink"));
  838. code_editor->get_text_edit()->cursor_set_blink_speed(EditorSettings::get_singleton()->get("text_editor/caret_blink_speed"));
  839. code_editor->get_text_edit()->set_draw_breakpoint_gutter(EditorSettings::get_singleton()->get("text_editor/show_breakpoint_gutter"));
  840. code_editor->get_text_edit()->cursor_set_block_mode(EditorSettings::get_singleton()->get("text_editor/block_caret"));
  841. code_editor->get_text_edit()->set_callhint_settings(
  842. EditorSettings::get_singleton()->get("text_editor/put_callhint_tooltip_below_current_line"),
  843. EditorSettings::get_singleton()->get("text_editor/callhint_tooltip_offset"));
  844. edit_hb = memnew (HBoxContainer);
  845. edit_menu = memnew( MenuButton );
  846. edit_menu->set_text(TTR("Edit"));
  847. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/undo"), EDIT_UNDO);
  848. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/redo"), EDIT_REDO);
  849. edit_menu->get_popup()->add_separator();
  850. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/cut"), EDIT_CUT);
  851. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/copy"), EDIT_COPY);
  852. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/paste"), EDIT_PASTE);
  853. edit_menu->get_popup()->add_separator();
  854. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/select_all"), EDIT_SELECT_ALL);
  855. edit_menu->get_popup()->add_separator();
  856. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/move_up"), EDIT_MOVE_LINE_UP);
  857. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/move_down"), EDIT_MOVE_LINE_DOWN);
  858. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/indent_left"), EDIT_INDENT_LEFT);
  859. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/indent_right"), EDIT_INDENT_RIGHT);
  860. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/toggle_comment"), EDIT_TOGGLE_COMMENT);
  861. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/clone_down"), EDIT_CLONE_DOWN);
  862. edit_menu->get_popup()->add_separator();
  863. #ifdef OSX_ENABLED
  864. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/complete_symbol"), EDIT_COMPLETE);
  865. #else
  866. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/complete_symbol"), EDIT_COMPLETE);
  867. #endif
  868. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/trim_trailing_whitespace"), EDIT_TRIM_TRAILING_WHITESAPCE);
  869. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/auto_indent"), EDIT_AUTO_INDENT);
  870. edit_menu->get_popup()->connect("item_pressed", this,"_edit_option");
  871. edit_menu->get_popup()->add_separator();
  872. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/toggle_breakpoint"), DEBUG_TOGGLE_BREAKPOINT);
  873. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/remove_all_breakpoints"), DEBUG_REMOVE_ALL_BREAKPOINTS);
  874. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/goto_next_breakpoint"), DEBUG_GOTO_NEXT_BREAKPOINT);
  875. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/goto_previous_breakpoint"), DEBUG_GOTO_PREV_BREAKPOINT);
  876. search_menu = memnew( MenuButton );
  877. edit_hb->add_child(search_menu);
  878. search_menu->set_text(TTR("Search"));
  879. search_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/find"), SEARCH_FIND);
  880. search_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/find_next"), SEARCH_FIND_NEXT);
  881. search_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/find_previous"), SEARCH_FIND_PREV);
  882. search_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/replace"), SEARCH_REPLACE);
  883. search_menu->get_popup()->add_separator();
  884. search_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/goto_function"), SEARCH_LOCATE_FUNCTION);
  885. search_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/goto_line"), SEARCH_GOTO_LINE);
  886. search_menu->get_popup()->add_separator();
  887. search_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/contextual_help"), HELP_CONTEXTUAL);
  888. search_menu->get_popup()->connect("item_pressed", this,"_edit_option");
  889. edit_hb->add_child(edit_menu);
  890. quick_open = memnew( ScriptEditorQuickOpen );
  891. add_child(quick_open);
  892. quick_open->connect("goto_line",this,"_goto_line");
  893. goto_line_dialog = memnew(GotoLineDialog);
  894. add_child(goto_line_dialog);
  895. code_editor->get_text_edit()->set_drag_forwarding(this);
  896. }
  897. static ScriptEditorBase * create_editor(const Ref<Script>& p_script) {
  898. if (p_script->has_source_code()) {
  899. return memnew( ScriptTextEditor );
  900. }
  901. return NULL;
  902. }
  903. void ScriptTextEditor::register_editor() {
  904. ED_SHORTCUT("script_text_editor/undo", TTR("Undo"), KEY_MASK_CMD|KEY_Z);
  905. ED_SHORTCUT("script_text_editor/redo", TTR("Redo"), KEY_MASK_CMD|KEY_Y);
  906. ED_SHORTCUT("script_text_editor/cut", TTR("Cut"), KEY_MASK_CMD|KEY_X);
  907. ED_SHORTCUT("script_text_editor/copy", TTR("Copy"), KEY_MASK_CMD|KEY_C);
  908. ED_SHORTCUT("script_text_editor/paste", TTR("Paste"), KEY_MASK_CMD|KEY_V);
  909. ED_SHORTCUT("script_text_editor/select_all", TTR("Select All"), KEY_MASK_CMD|KEY_A);
  910. ED_SHORTCUT("script_text_editor/move_up", TTR("Move Up"), KEY_MASK_ALT|KEY_UP);
  911. ED_SHORTCUT("script_text_editor/move_down", TTR("Move Down"), KEY_MASK_ALT|KEY_DOWN);
  912. ED_SHORTCUT("script_text_editor/indent_left", TTR("Indent Left"), 0);
  913. ED_SHORTCUT("script_text_editor/indent_right", TTR("Indent Right"), 0);
  914. ED_SHORTCUT("script_text_editor/toggle_comment", TTR("Toggle Comment"), KEY_MASK_CMD|KEY_K);
  915. ED_SHORTCUT("script_text_editor/clone_down", TTR("Clone Down"), KEY_MASK_CMD|KEY_B);
  916. #ifdef OSX_ENABLED
  917. ED_SHORTCUT("script_text_editor/complete_symbol", TTR("Complete Symbol"), KEY_MASK_CTRL|KEY_SPACE);
  918. #else
  919. ED_SHORTCUT("script_text_editor/complete_symbol", TTR("Complete Symbol"), KEY_MASK_CMD|KEY_SPACE);
  920. #endif
  921. ED_SHORTCUT("script_text_editor/trim_trailing_whitespace", TTR("Trim Trailing Whitespace"), KEY_MASK_CTRL|KEY_MASK_ALT|KEY_T);
  922. ED_SHORTCUT("script_text_editor/auto_indent", TTR("Auto Indent"), KEY_MASK_CMD|KEY_I);
  923. ED_SHORTCUT("script_text_editor/toggle_breakpoint", TTR("Toggle Breakpoint"), KEY_F9);
  924. ED_SHORTCUT("script_text_editor/remove_all_breakpoints", TTR("Remove All Breakpoints"), KEY_MASK_CTRL|KEY_MASK_SHIFT|KEY_F9);
  925. ED_SHORTCUT("script_text_editor/goto_next_breakpoint", TTR("Goto Next Breakpoint"), KEY_MASK_CTRL|KEY_PERIOD);
  926. ED_SHORTCUT("script_text_editor/goto_previous_breakpoint", TTR("Goto Previous Breakpoint"), KEY_MASK_CTRL|KEY_COMMA);
  927. ED_SHORTCUT("script_text_editor/find", TTR("Find.."), KEY_MASK_CMD|KEY_F);
  928. ED_SHORTCUT("script_text_editor/find_next", TTR("Find Next"), KEY_F3);
  929. ED_SHORTCUT("script_text_editor/find_previous", TTR("Find Previous"), KEY_MASK_SHIFT|KEY_F3);
  930. ED_SHORTCUT("script_text_editor/replace", TTR("Replace.."), KEY_MASK_CMD|KEY_R);
  931. ED_SHORTCUT("script_text_editor/goto_function", TTR("Goto Function.."), KEY_MASK_SHIFT|KEY_MASK_CMD|KEY_F);
  932. ED_SHORTCUT("script_text_editor/goto_line", TTR("Goto Line.."), KEY_MASK_CMD|KEY_L);
  933. ED_SHORTCUT("script_text_editor/contextual_help", TTR("Contextual Help"), KEY_MASK_SHIFT|KEY_F1);
  934. ScriptEditor::register_create_script_editor_function(create_editor);
  935. }