script_text_editor.cpp 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422
  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_show_line_length_guideline(EditorSettings::get_singleton()->get("text_editor/show_line_length_guideline"));
  197. code_editor->get_text_edit()->set_line_length_guideline_column(EditorSettings::get_singleton()->get("text_editor/line_length_guideline_column"));
  198. code_editor->get_text_edit()->set_syntax_coloring(EditorSettings::get_singleton()->get("text_editor/syntax_highlighting"));
  199. code_editor->get_text_edit()->set_highlight_all_occurrences(EditorSettings::get_singleton()->get("text_editor/highlight_all_occurrences"));
  200. code_editor->get_text_edit()->cursor_set_blink_enabled(EditorSettings::get_singleton()->get("text_editor/caret_blink"));
  201. code_editor->get_text_edit()->cursor_set_blink_speed(EditorSettings::get_singleton()->get("text_editor/caret_blink_speed"));
  202. code_editor->get_text_edit()->set_draw_breakpoint_gutter(EditorSettings::get_singleton()->get("text_editor/show_breakpoint_gutter"));
  203. code_editor->get_text_edit()->cursor_set_block_mode(EditorSettings::get_singleton()->get("text_editor/block_caret"));
  204. }
  205. bool ScriptTextEditor::is_unsaved() {
  206. return code_editor->get_text_edit()->get_version()!=code_editor->get_text_edit()->get_saved_version();
  207. }
  208. Variant ScriptTextEditor::get_edit_state() {
  209. Dictionary state;
  210. state["scroll_pos"]=code_editor->get_text_edit()->get_v_scroll();
  211. state["column"]=code_editor->get_text_edit()->cursor_get_column();
  212. state["row"]=code_editor->get_text_edit()->cursor_get_line();
  213. return state;
  214. }
  215. void ScriptTextEditor::trim_trailing_whitespace() {
  216. TextEdit *tx = code_editor->get_text_edit();
  217. bool trimed_whitespace = false;
  218. for (int i = 0; i < tx->get_line_count(); i++) {
  219. String line = tx->get_line(i);
  220. if (line.ends_with(" ") || line.ends_with("\t")) {
  221. if (!trimed_whitespace) {
  222. tx->begin_complex_operation();
  223. trimed_whitespace = true;
  224. }
  225. int end = 0;
  226. for (int j = line.length() - 1; j > -1; j--) {
  227. if (line[j] != ' ' && line[j] != '\t') {
  228. end = j+1;
  229. break;
  230. }
  231. }
  232. tx->set_line(i, line.substr(0, end));
  233. }
  234. }
  235. if (trimed_whitespace) {
  236. tx->end_complex_operation();
  237. tx->update();
  238. }
  239. }
  240. void ScriptTextEditor::tag_saved_version() {
  241. code_editor->get_text_edit()->tag_saved_version();
  242. }
  243. void ScriptTextEditor::goto_line(int p_line, bool p_with_error) {
  244. code_editor->get_text_edit()->cursor_set_line(p_line);
  245. }
  246. void ScriptTextEditor::ensure_focus() {
  247. code_editor->get_text_edit()->grab_focus();
  248. }
  249. void ScriptTextEditor::set_edit_state(const Variant& p_state) {
  250. Dictionary state=p_state;
  251. code_editor->get_text_edit()->set_v_scroll(state["scroll_pos"]);
  252. code_editor->get_text_edit()->cursor_set_column( state["column"]);
  253. code_editor->get_text_edit()->cursor_set_line( state["row"] );
  254. code_editor->get_text_edit()->grab_focus();
  255. //int scroll_pos;
  256. //int cursor_column;
  257. //int cursor_row;
  258. }
  259. String ScriptTextEditor::get_name() {
  260. String name;
  261. if (script->get_path().find("local://")==-1 && script->get_path().find("::")==-1) {
  262. name=script->get_path().get_file();
  263. if (is_unsaved()) {
  264. name+="(*)";
  265. }
  266. } else if (script->get_name()!="")
  267. name=script->get_name();
  268. else
  269. name=script->get_type()+"("+itos(script->get_instance_ID())+")";
  270. return name;
  271. }
  272. Ref<Texture> ScriptTextEditor::get_icon() {
  273. if (get_parent_control() && get_parent_control()->has_icon(script->get_type(),"EditorIcons")) {
  274. return get_parent_control()->get_icon(script->get_type(),"EditorIcons");
  275. }
  276. return Ref<Texture>();
  277. }
  278. void ScriptTextEditor::set_edited_script(const Ref<Script>& p_script) {
  279. ERR_FAIL_COND(!script.is_null());
  280. script=p_script;
  281. _load_theme_settings();
  282. code_editor->get_text_edit()->set_text(script->get_source_code());
  283. code_editor->get_text_edit()->clear_undo_history();
  284. code_editor->get_text_edit()->tag_saved_version();
  285. emit_signal("name_changed");
  286. code_editor->update_line_and_column();
  287. }
  288. void ScriptTextEditor::_validate_script() {
  289. String errortxt;
  290. int line=-1,col;
  291. TextEdit *te=code_editor->get_text_edit();
  292. String text = te->get_text();
  293. List<String> fnc;
  294. if (!script->get_language()->validate(text,line,col,errortxt,script->get_path(),&fnc)) {
  295. String error_text="error("+itos(line)+","+itos(col)+"): "+errortxt;
  296. code_editor->set_error(error_text);
  297. } else {
  298. code_editor->set_error("");
  299. line=-1;
  300. if (!script->is_tool()) {
  301. script->set_source_code(text);
  302. script->update_exports();
  303. //script->reload(); //will update all the variables in property editors
  304. }
  305. functions.clear();
  306. for (List<String>::Element *E=fnc.front();E;E=E->next()) {
  307. functions.push_back(E->get());
  308. }
  309. }
  310. line--;
  311. for(int i=0;i<te->get_line_count();i++) {
  312. te->set_line_as_marked(i,line==i);
  313. }
  314. emit_signal("name_changed");
  315. }
  316. static Node* _find_node_for_script(Node* p_base, Node*p_current, const Ref<Script>& p_script) {
  317. if (p_current->get_owner()!=p_base && p_base!=p_current)
  318. return NULL;
  319. Ref<Script> c = p_current->get_script();
  320. if (c==p_script)
  321. return p_current;
  322. for(int i=0;i<p_current->get_child_count();i++) {
  323. Node *found = _find_node_for_script(p_base,p_current->get_child(i),p_script);
  324. if (found)
  325. return found;
  326. }
  327. return NULL;
  328. }
  329. static void _find_changed_scripts_for_external_editor(Node* p_base, Node*p_current, Set<Ref<Script> > &r_scripts) {
  330. if (p_current->get_owner()!=p_base && p_base!=p_current)
  331. return;
  332. Ref<Script> c = p_current->get_script();
  333. if (c.is_valid())
  334. r_scripts.insert(c);
  335. for(int i=0;i<p_current->get_child_count();i++) {
  336. _find_changed_scripts_for_external_editor(p_base,p_current->get_child(i),r_scripts);
  337. }
  338. }
  339. void ScriptEditor::_update_modified_scripts_for_external_editor(Ref<Script> p_for_script) {
  340. if (!bool(EditorSettings::get_singleton()->get("external_editor/use_external_editor")))
  341. return;
  342. Set<Ref<Script> > scripts;
  343. Node *base = get_tree()->get_edited_scene_root();
  344. if (base) {
  345. _find_changed_scripts_for_external_editor(base,base,scripts);
  346. }
  347. for (Set<Ref<Script> >::Element *E=scripts.front();E;E=E->next()) {
  348. Ref<Script> script = E->get();
  349. if (p_for_script.is_valid() && p_for_script!=script)
  350. continue;
  351. if (script->get_path()=="" || script->get_path().find("local://")!=-1 || script->get_path().find("::")!=-1) {
  352. continue; //internal script, who cares, though weird
  353. }
  354. uint64_t last_date = script->get_last_modified_time();
  355. uint64_t date = FileAccess::get_modified_time(script->get_path());
  356. if (last_date!=date) {
  357. Ref<Script> rel_script = ResourceLoader::load(script->get_path(),script->get_type(),true);
  358. ERR_CONTINUE(!rel_script.is_valid());
  359. script->set_source_code( rel_script->get_source_code() );
  360. script->set_last_modified_time( rel_script->get_last_modified_time() );
  361. script->update_exports();
  362. }
  363. }
  364. }
  365. void ScriptTextEditor::_code_complete_scripts(void* p_ud,const String& p_code, List<String>* r_options) {
  366. ScriptTextEditor *ste = (ScriptTextEditor *)p_ud;
  367. ste->_code_complete_script(p_code,r_options);
  368. }
  369. void ScriptTextEditor::_code_complete_script(const String& p_code, List<String>* r_options) {
  370. if (color_panel->is_visible()) return;
  371. Node *base = get_tree()->get_edited_scene_root();
  372. if (base) {
  373. base = _find_node_for_script(base,base,script);
  374. }
  375. String hint;
  376. Error err = script->get_language()->complete_code(p_code,script->get_path().get_base_dir(),base,r_options,hint);
  377. if (hint!="") {
  378. code_editor->get_text_edit()->set_code_hint(hint);
  379. }
  380. }
  381. void ScriptTextEditor::_breakpoint_toggled(int p_row) {
  382. 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));
  383. }
  384. static void swap_lines(TextEdit *tx, int line1, int line2)
  385. {
  386. String tmp = tx->get_line(line1);
  387. String tmp2 = tx->get_line(line2);
  388. tx->set_line(line2, tmp);
  389. tx->set_line(line1, tmp2);
  390. tx->cursor_set_line(line2);
  391. }
  392. void ScriptTextEditor::_lookup_symbol(const String& p_symbol,int p_row, int p_column) {
  393. Node *base = get_tree()->get_edited_scene_root();
  394. if (base) {
  395. base = _find_node_for_script(base,base,script);
  396. }
  397. ScriptLanguage::LookupResult result;
  398. 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) {
  399. _goto_line(p_row);
  400. switch(result.type) {
  401. case ScriptLanguage::LookupResult::RESULT_SCRIPT_LOCATION: {
  402. if (result.script.is_valid()) {
  403. emit_signal("request_open_script_at_line",result.script,result.location-1);
  404. } else {
  405. emit_signal("request_save_history");
  406. _goto_line(result.location-1);
  407. }
  408. } break;
  409. case ScriptLanguage::LookupResult::RESULT_CLASS: {
  410. emit_signal("go_to_help","class_name:"+result.class_name);
  411. } break;
  412. case ScriptLanguage::LookupResult::RESULT_CLASS_CONSTANT: {
  413. StringName cname = result.class_name;
  414. bool success;
  415. while(true) {
  416. ObjectTypeDB::get_integer_constant(cname,result.class_member,&success);
  417. if (success) {
  418. result.class_name=cname;
  419. cname=ObjectTypeDB::type_inherits_from(cname);
  420. } else {
  421. break;
  422. }
  423. }
  424. emit_signal("go_to_help","class_constant:"+result.class_name+":"+result.class_member);
  425. } break;
  426. case ScriptLanguage::LookupResult::RESULT_CLASS_PROPERTY: {
  427. emit_signal("go_to_help","class_property:"+result.class_name+":"+result.class_member);
  428. } break;
  429. case ScriptLanguage::LookupResult::RESULT_CLASS_METHOD: {
  430. StringName cname = result.class_name;
  431. while(true) {
  432. if (ObjectTypeDB::has_method(cname,result.class_member)) {
  433. result.class_name=cname;
  434. cname=ObjectTypeDB::type_inherits_from(cname);
  435. } else {
  436. break;
  437. }
  438. }
  439. emit_signal("go_to_help","class_method:"+result.class_name+":"+result.class_member);
  440. } break;
  441. }
  442. }
  443. }
  444. void ScriptTextEditor::_edit_option(int p_op) {
  445. switch(p_op) {
  446. case EDIT_UNDO: {
  447. code_editor->get_text_edit()->undo();
  448. code_editor->get_text_edit()->call_deferred("grab_focus");
  449. } break;
  450. case EDIT_REDO: {
  451. code_editor->get_text_edit()->redo();
  452. code_editor->get_text_edit()->call_deferred("grab_focus");
  453. } break;
  454. case EDIT_CUT: {
  455. code_editor->get_text_edit()->cut();
  456. code_editor->get_text_edit()->call_deferred("grab_focus");
  457. } break;
  458. case EDIT_COPY: {
  459. code_editor->get_text_edit()->copy();
  460. code_editor->get_text_edit()->call_deferred("grab_focus");
  461. } break;
  462. case EDIT_PASTE: {
  463. code_editor->get_text_edit()->paste();
  464. code_editor->get_text_edit()->call_deferred("grab_focus");
  465. } break;
  466. case EDIT_SELECT_ALL: {
  467. code_editor->get_text_edit()->select_all();
  468. code_editor->get_text_edit()->call_deferred("grab_focus");
  469. } break;
  470. case EDIT_MOVE_LINE_UP: {
  471. TextEdit *tx = code_editor->get_text_edit();
  472. Ref<Script> scr = script;
  473. if (scr.is_null())
  474. return;
  475. tx->begin_complex_operation();
  476. if (tx->is_selection_active())
  477. {
  478. int from_line = tx->get_selection_from_line();
  479. int from_col = tx->get_selection_from_column();
  480. int to_line = tx->get_selection_to_line();
  481. int to_column = tx->get_selection_to_column();
  482. for (int i = from_line; i <= to_line; i++)
  483. {
  484. int line_id = i;
  485. int next_id = i - 1;
  486. if (line_id == 0 || next_id < 0)
  487. return;
  488. swap_lines(tx, line_id, next_id);
  489. }
  490. int from_line_up = from_line > 0 ? from_line-1 : from_line;
  491. int to_line_up = to_line > 0 ? to_line-1 : to_line;
  492. tx->select(from_line_up, from_col, to_line_up, to_column);
  493. }
  494. else
  495. {
  496. int line_id = tx->cursor_get_line();
  497. int next_id = line_id - 1;
  498. if (line_id == 0 || next_id < 0)
  499. return;
  500. swap_lines(tx, line_id, next_id);
  501. }
  502. tx->end_complex_operation();
  503. tx->update();
  504. } break;
  505. case EDIT_MOVE_LINE_DOWN: {
  506. TextEdit *tx = code_editor->get_text_edit();
  507. Ref<Script> scr = get_edited_script();
  508. if (scr.is_null())
  509. return;
  510. tx->begin_complex_operation();
  511. if (tx->is_selection_active())
  512. {
  513. int from_line = tx->get_selection_from_line();
  514. int from_col = tx->get_selection_from_column();
  515. int to_line = tx->get_selection_to_line();
  516. int to_column = tx->get_selection_to_column();
  517. for (int i = to_line; i >= from_line; i--)
  518. {
  519. int line_id = i;
  520. int next_id = i + 1;
  521. if (line_id == tx->get_line_count()-1 || next_id > tx->get_line_count())
  522. return;
  523. swap_lines(tx, line_id, next_id);
  524. }
  525. int from_line_down = from_line < tx->get_line_count() ? from_line+1 : from_line;
  526. int to_line_down = to_line < tx->get_line_count() ? to_line+1 : to_line;
  527. tx->select(from_line_down, from_col, to_line_down, to_column);
  528. }
  529. else
  530. {
  531. int line_id = tx->cursor_get_line();
  532. int next_id = line_id + 1;
  533. if (line_id == tx->get_line_count()-1 || next_id > tx->get_line_count())
  534. return;
  535. swap_lines(tx, line_id, next_id);
  536. }
  537. tx->end_complex_operation();
  538. tx->update();
  539. } break;
  540. case EDIT_INDENT_LEFT: {
  541. TextEdit *tx = code_editor->get_text_edit();
  542. Ref<Script> scr = get_edited_script();
  543. if (scr.is_null())
  544. return;
  545. tx->begin_complex_operation();
  546. if (tx->is_selection_active())
  547. {
  548. tx->indent_selection_left();
  549. }
  550. else
  551. {
  552. int begin = tx->cursor_get_line();
  553. String line_text = tx->get_line(begin);
  554. // begins with tab
  555. if (line_text.begins_with("\t"))
  556. {
  557. line_text = line_text.substr(1, line_text.length());
  558. tx->set_line(begin, line_text);
  559. }
  560. // begins with 4 spaces
  561. else if (line_text.begins_with(" "))
  562. {
  563. line_text = line_text.substr(4, line_text.length());
  564. tx->set_line(begin, line_text);
  565. }
  566. }
  567. tx->end_complex_operation();
  568. tx->update();
  569. //tx->deselect();
  570. } break;
  571. case EDIT_INDENT_RIGHT: {
  572. TextEdit *tx = code_editor->get_text_edit();
  573. Ref<Script> scr = get_edited_script();
  574. if (scr.is_null())
  575. return;
  576. tx->begin_complex_operation();
  577. if (tx->is_selection_active())
  578. {
  579. tx->indent_selection_right();
  580. }
  581. else
  582. {
  583. int begin = tx->cursor_get_line();
  584. String line_text = tx->get_line(begin);
  585. line_text = '\t' + line_text;
  586. tx->set_line(begin, line_text);
  587. }
  588. tx->end_complex_operation();
  589. tx->update();
  590. //tx->deselect();
  591. } break;
  592. case EDIT_CLONE_DOWN: {
  593. TextEdit *tx = code_editor->get_text_edit();
  594. Ref<Script> scr = get_edited_script();
  595. if (scr.is_null())
  596. return;
  597. int from_line = tx->cursor_get_line();
  598. int to_line = tx->cursor_get_line();
  599. int column = tx->cursor_get_column();
  600. if (tx->is_selection_active()) {
  601. from_line = tx->get_selection_from_line();
  602. to_line = tx->get_selection_to_line();
  603. column = tx->cursor_get_column();
  604. }
  605. int next_line = to_line + 1;
  606. tx->begin_complex_operation();
  607. for (int i = from_line; i <= to_line; i++) {
  608. if (i >= tx->get_line_count() - 1) {
  609. tx->set_line(i, tx->get_line(i) + "\n");
  610. }
  611. String line_clone = tx->get_line(i);
  612. tx->insert_at(line_clone, next_line);
  613. next_line++;
  614. }
  615. tx->cursor_set_column(column);
  616. if (tx->is_selection_active()) {
  617. tx->select(to_line + 1, tx->get_selection_from_column(), next_line - 1, tx->get_selection_to_column());
  618. }
  619. tx->end_complex_operation();
  620. tx->update();
  621. } break;
  622. case EDIT_TOGGLE_COMMENT: {
  623. TextEdit *tx = code_editor->get_text_edit();
  624. Ref<Script> scr = get_edited_script();
  625. if (scr.is_null())
  626. return;
  627. tx->begin_complex_operation();
  628. if (tx->is_selection_active())
  629. {
  630. int begin = tx->get_selection_from_line();
  631. int end = tx->get_selection_to_line();
  632. // End of selection ends on the first column of the last line, ignore it.
  633. if(tx->get_selection_to_column() == 0)
  634. end -= 1;
  635. for (int i = begin; i <= end; i++)
  636. {
  637. String line_text = tx->get_line(i);
  638. if (line_text.begins_with("#"))
  639. line_text = line_text.substr(1, line_text.length());
  640. else
  641. line_text = "#" + line_text;
  642. tx->set_line(i, line_text);
  643. }
  644. }
  645. else
  646. {
  647. int begin = tx->cursor_get_line();
  648. String line_text = tx->get_line(begin);
  649. if (line_text.begins_with("#"))
  650. line_text = line_text.substr(1, line_text.length());
  651. else
  652. line_text = "#" + line_text;
  653. tx->set_line(begin, line_text);
  654. }
  655. tx->end_complex_operation();
  656. tx->update();
  657. //tx->deselect();
  658. } break;
  659. case EDIT_COMPLETE: {
  660. code_editor->get_text_edit()->query_code_comple();
  661. } break;
  662. case EDIT_AUTO_INDENT: {
  663. TextEdit *te = code_editor->get_text_edit();
  664. String text = te->get_text();
  665. Ref<Script> scr = get_edited_script();
  666. if (scr.is_null())
  667. return;
  668. int begin,end;
  669. if (te->is_selection_active()) {
  670. begin=te->get_selection_from_line();
  671. end=te->get_selection_to_line();
  672. } else {
  673. begin=0;
  674. end=te->get_line_count()-1;
  675. }
  676. scr->get_language()->auto_indent_code(text,begin,end);
  677. te->set_text(text);
  678. } break;
  679. case EDIT_TRIM_TRAILING_WHITESAPCE: {
  680. trim_trailing_whitespace();
  681. } break;
  682. case EDIT_PICK_COLOR: {
  683. color_panel->popup();
  684. } break;
  685. case SEARCH_FIND: {
  686. code_editor->get_find_replace_bar()->popup_search();
  687. } break;
  688. case SEARCH_FIND_NEXT: {
  689. code_editor->get_find_replace_bar()->search_next();
  690. } break;
  691. case SEARCH_FIND_PREV: {
  692. code_editor->get_find_replace_bar()->search_prev();
  693. } break;
  694. case SEARCH_REPLACE: {
  695. code_editor->get_find_replace_bar()->popup_replace();
  696. } break;
  697. case SEARCH_LOCATE_FUNCTION: {
  698. quick_open->popup(get_functions());
  699. } break;
  700. case SEARCH_GOTO_LINE: {
  701. goto_line_dialog->popup_find_line(code_editor->get_text_edit());
  702. } break;
  703. case DEBUG_TOGGLE_BREAKPOINT: {
  704. int line=code_editor->get_text_edit()->cursor_get_line();
  705. bool dobreak = !code_editor->get_text_edit()->is_line_set_as_breakpoint(line);
  706. code_editor->get_text_edit()->set_line_as_breakpoint(line,dobreak);
  707. ScriptEditor::get_singleton()->get_debugger()->set_breakpoint(get_edited_script()->get_path(),line+1,dobreak);
  708. } break;
  709. case DEBUG_REMOVE_ALL_BREAKPOINTS: {
  710. List<int> bpoints;
  711. code_editor->get_text_edit()->get_breakpoints(&bpoints);
  712. for(List<int>::Element *E=bpoints.front();E;E=E->next()) {
  713. int line = E->get();
  714. bool dobreak = !code_editor->get_text_edit()->is_line_set_as_breakpoint(line);
  715. code_editor->get_text_edit()->set_line_as_breakpoint(line,dobreak);
  716. ScriptEditor::get_singleton()->get_debugger()->set_breakpoint(get_edited_script()->get_path(),line+1,dobreak);
  717. }
  718. }
  719. case DEBUG_GOTO_NEXT_BREAKPOINT: {
  720. List<int> bpoints;
  721. code_editor->get_text_edit()->get_breakpoints(&bpoints);
  722. if (bpoints.size() <= 0) {
  723. return;
  724. }
  725. int line=code_editor->get_text_edit()->cursor_get_line();
  726. // wrap around
  727. if (line >= bpoints[bpoints.size() - 1]) {
  728. code_editor->get_text_edit()->cursor_set_line(bpoints[0]);
  729. } else {
  730. for(List<int>::Element *E=bpoints.front();E;E=E->next()) {
  731. int bline = E->get();
  732. if (bline > line) {
  733. code_editor->get_text_edit()->cursor_set_line(bline);
  734. return;
  735. }
  736. }
  737. }
  738. } break;
  739. case DEBUG_GOTO_PREV_BREAKPOINT: {
  740. List<int> bpoints;
  741. code_editor->get_text_edit()->get_breakpoints(&bpoints);
  742. if (bpoints.size() <= 0) {
  743. return;
  744. }
  745. int line=code_editor->get_text_edit()->cursor_get_line();
  746. // wrap around
  747. if (line <= bpoints[0]) {
  748. code_editor->get_text_edit()->cursor_set_line(bpoints[bpoints.size() - 1]);
  749. } else {
  750. for(List<int>::Element *E=bpoints.back();E;E=E->prev()) {
  751. int bline = E->get();
  752. if (bline < line) {
  753. code_editor->get_text_edit()->cursor_set_line(bline);
  754. return;
  755. }
  756. }
  757. }
  758. } break;
  759. case HELP_CONTEXTUAL: {
  760. String text = code_editor->get_text_edit()->get_selection_text();
  761. if (text == "")
  762. text = code_editor->get_text_edit()->get_word_under_cursor();
  763. if (text != "") {
  764. emit_signal("request_help_search",text);
  765. }
  766. } break;
  767. }
  768. }
  769. void ScriptTextEditor::_bind_methods() {
  770. ObjectTypeDB::bind_method("_validate_script",&ScriptTextEditor::_validate_script);
  771. ObjectTypeDB::bind_method("_load_theme_settings",&ScriptTextEditor::_load_theme_settings);
  772. ObjectTypeDB::bind_method("_breakpoint_toggled",&ScriptTextEditor::_breakpoint_toggled);
  773. ObjectTypeDB::bind_method("_edit_option",&ScriptTextEditor::_edit_option);
  774. ObjectTypeDB::bind_method("_goto_line",&ScriptTextEditor::_goto_line);
  775. ObjectTypeDB::bind_method("_lookup_symbol",&ScriptTextEditor::_lookup_symbol);
  776. ObjectTypeDB::bind_method("_text_edit_input_event", &ScriptTextEditor::_text_edit_input_event);
  777. ObjectTypeDB::bind_method("_color_changed", &ScriptTextEditor::_color_changed);
  778. ObjectTypeDB::bind_method("get_drag_data_fw",&ScriptTextEditor::get_drag_data_fw);
  779. ObjectTypeDB::bind_method("can_drop_data_fw",&ScriptTextEditor::can_drop_data_fw);
  780. ObjectTypeDB::bind_method("drop_data_fw",&ScriptTextEditor::drop_data_fw);
  781. }
  782. Control *ScriptTextEditor::get_edit_menu() {
  783. return edit_hb;
  784. }
  785. void ScriptTextEditor::reload(bool p_soft) {
  786. TextEdit *te = code_editor->get_text_edit();
  787. Ref<Script> scr = get_edited_script();
  788. if (scr.is_null())
  789. return;
  790. scr->set_source_code(te->get_text());
  791. bool soft = p_soft || scr->get_instance_base_type()=="EditorPlugin"; //always soft-reload editor plugins
  792. scr->get_language()->reload_tool_script(scr,soft);
  793. }
  794. void ScriptTextEditor::get_breakpoints(List<int> *p_breakpoints) {
  795. code_editor->get_text_edit()->get_breakpoints(p_breakpoints);
  796. }
  797. void ScriptTextEditor::set_tooltip_request_func(String p_method,Object* p_obj) {
  798. code_editor->get_text_edit()->set_tooltip_request_func(p_obj,p_method,this);
  799. }
  800. void ScriptTextEditor::set_debugger_active(bool p_active) {
  801. }
  802. Variant ScriptTextEditor::get_drag_data_fw(const Point2& p_point,Control* p_from) {
  803. return Variant();
  804. }
  805. bool ScriptTextEditor::can_drop_data_fw(const Point2& p_point,const Variant& p_data,Control* p_from) const{
  806. Dictionary d = p_data;
  807. if (d.has("type") &&
  808. (
  809. String(d["type"])=="resource" ||
  810. String(d["type"])=="files" ||
  811. String(d["type"])=="nodes"
  812. ) ) {
  813. return true;
  814. }
  815. return false;
  816. }
  817. #ifdef TOOLS_ENABLED
  818. static Node* _find_script_node(Node* p_edited_scene,Node* p_current_node,const Ref<Script> &script) {
  819. if (p_edited_scene!=p_current_node && p_current_node->get_owner()!=p_edited_scene)
  820. return NULL;
  821. Ref<Script> scr = p_current_node->get_script();
  822. if (scr.is_valid() && scr==script)
  823. return p_current_node;
  824. for(int i=0;i<p_current_node->get_child_count();i++) {
  825. Node *n = _find_script_node(p_edited_scene,p_current_node->get_child(i),script);
  826. if (n)
  827. return n;
  828. }
  829. return NULL;
  830. }
  831. #else
  832. static Node* _find_script_node(Node* p_edited_scene,Node* p_current_node,const Ref<Script> &script) {
  833. return NULL;
  834. }
  835. #endif
  836. void ScriptTextEditor::drop_data_fw(const Point2& p_point,const Variant& p_data,Control* p_from){
  837. Dictionary d = p_data;
  838. if (d.has("type") && String(d["type"])=="resource") {
  839. Ref<Resource> res = d["resource"];
  840. if (!res.is_valid()) {
  841. return;
  842. }
  843. if (res->get_path().is_resource_file()) {
  844. EditorNode::get_singleton()->show_warning("Only resources from filesystem can be dropped.");
  845. return;
  846. }
  847. code_editor->get_text_edit()->insert_text_at_cursor(res->get_path());
  848. }
  849. if (d.has("type") && String(d["type"])=="files") {
  850. Array files = d["files"];
  851. String text_to_drop;
  852. for(int i=0;i<files.size();i++) {
  853. if (i>0)
  854. text_to_drop+=",";
  855. text_to_drop+="\""+String(files[i]).c_escape()+"\"";
  856. }
  857. code_editor->get_text_edit()->insert_text_at_cursor(text_to_drop);
  858. }
  859. if (d.has("type") && String(d["type"])=="nodes") {
  860. Node* sn = _find_script_node(get_tree()->get_edited_scene_root(),get_tree()->get_edited_scene_root(),script);
  861. if (!sn) {
  862. EditorNode::get_singleton()->show_warning("Can't drop nodes because script '"+get_name()+"' is not used in this scene.");
  863. return;
  864. }
  865. Array nodes = d["nodes"];
  866. String text_to_drop;
  867. for(int i=0;i<nodes.size();i++) {
  868. if (i>0)
  869. text_to_drop+=",";
  870. NodePath np = nodes[i];
  871. Node *node = get_node(np);
  872. if (!node) {
  873. continue;
  874. }
  875. String path = sn->get_path_to(node);
  876. text_to_drop+="\""+path.c_escape()+"\"";
  877. }
  878. code_editor->get_text_edit()->insert_text_at_cursor(text_to_drop);
  879. }
  880. }
  881. void ScriptTextEditor::_text_edit_input_event(const InputEvent& ev) {
  882. if (ev.type == InputEvent::MOUSE_BUTTON) {
  883. InputEventMouseButton mb = ev.mouse_button;
  884. if (mb.button_index == BUTTON_RIGHT && !mb.pressed) {
  885. int col, row;
  886. TextEdit* tx = code_editor->get_text_edit();
  887. tx->_get_mouse_pos(Point2i(mb.global_x, mb.global_y)-tx->get_global_pos(), row, col);
  888. Vector2 mpos = Vector2(mb.global_x, mb.global_y)-tx->get_global_pos();
  889. bool have_selection = (tx->get_selection_text().length() > 0);
  890. bool have_color = (tx->get_word_at_pos(mpos) == "Color");
  891. if (have_color) {
  892. String line = tx->get_line(row);
  893. color_line = row;
  894. int begin = 0;
  895. int end = 0;
  896. bool valid = false;
  897. for (int i = col; i < line.length(); i++) {
  898. if (line[i] == '(') {
  899. begin = i;
  900. continue;
  901. }
  902. else if (line[i] == ')') {
  903. end = i+1;
  904. valid = true;
  905. break;
  906. }
  907. }
  908. if (valid) {
  909. color_args = line.substr(begin, end-begin);
  910. String stripped = color_args.replace(" ", "").replace("(", "").replace(")", "");
  911. Vector<float> color = stripped.split_floats(",");
  912. if (color.size() > 2) {
  913. float alpha = color.size() > 3 ? color[3] : 1.0f;
  914. color_picker->set_color(Color(color[0], color[1], color[2], alpha));
  915. }
  916. color_panel->set_pos(get_global_transform().xform(get_local_mouse_pos()));
  917. Size2 ms = Size2(300, color_picker->get_combined_minimum_size().height+10);
  918. color_panel->set_size(ms);
  919. } else {
  920. have_color = false;
  921. }
  922. }
  923. _make_context_menu(have_selection, have_color);
  924. }
  925. }
  926. }
  927. void ScriptTextEditor::_color_changed(const Color& p_color) {
  928. String new_args;
  929. if (p_color.a == 1.0f) {
  930. new_args = String("("+rtos(p_color.r)+", "+rtos(p_color.g)+", "+rtos(p_color.b)+")");
  931. } else {
  932. new_args = String("("+rtos(p_color.r)+", "+rtos(p_color.g)+", "+rtos(p_color.b)+", "+rtos(p_color.a)+")");
  933. }
  934. String line = code_editor->get_text_edit()->get_line(color_line);
  935. String new_line = line.replace(color_args, new_args);
  936. color_args = new_args;
  937. code_editor->get_text_edit()->set_line(color_line, new_line);
  938. }
  939. void ScriptTextEditor::_make_context_menu(bool p_selection, bool p_color) {
  940. context_menu->clear();
  941. if (p_selection) {
  942. context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/cut"));
  943. context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/copy"));
  944. }
  945. context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/paste"));
  946. context_menu->add_separator();
  947. context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/select_all"));
  948. context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/undo"));
  949. if (p_selection) {
  950. context_menu->add_separator();
  951. context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/indent_left"));
  952. context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/indent_right"));
  953. context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/toggle_comment"));
  954. }
  955. if (p_color) {
  956. context_menu->add_separator();
  957. context_menu->add_item(TTR("Pick Color"), EDIT_PICK_COLOR);
  958. }
  959. context_menu->set_pos(get_global_transform().xform(get_local_mouse_pos()));
  960. context_menu->set_size(Vector2(1, 1));
  961. context_menu->popup();
  962. }
  963. ScriptTextEditor::ScriptTextEditor() {
  964. code_editor = memnew( CodeTextEditor );
  965. add_child(code_editor);
  966. code_editor->set_area_as_parent_rect();
  967. code_editor->connect("validate_script",this,"_validate_script");
  968. code_editor->connect("load_theme_settings",this,"_load_theme_settings");
  969. code_editor->set_code_complete_func(_code_complete_scripts,this);
  970. code_editor->get_text_edit()->connect("breakpoint_toggled", this, "_breakpoint_toggled");
  971. code_editor->get_text_edit()->connect("symbol_lookup", this, "_lookup_symbol");
  972. code_editor->get_text_edit()->set_scroll_pass_end_of_file(EditorSettings::get_singleton()->get("text_editor/scroll_past_end_of_file"));
  973. code_editor->get_text_edit()->set_auto_brace_completion(EditorSettings::get_singleton()->get("text_editor/auto_brace_complete"));
  974. code_editor->get_text_edit()->set_tab_size(EditorSettings::get_singleton()->get("text_editor/tab_size"));
  975. code_editor->get_text_edit()->set_draw_tabs(EditorSettings::get_singleton()->get("text_editor/draw_tabs"));
  976. code_editor->get_text_edit()->set_show_line_numbers(EditorSettings::get_singleton()->get("text_editor/show_line_numbers"));
  977. code_editor->get_text_edit()->set_show_line_length_guideline(EditorSettings::get_singleton()->get("text_editor/show_line_length_guideline"));
  978. code_editor->get_text_edit()->set_line_length_guideline_column(EditorSettings::get_singleton()->get("text_editor/line_length_guideline_column"));
  979. code_editor->get_text_edit()->set_syntax_coloring(EditorSettings::get_singleton()->get("text_editor/syntax_highlighting"));
  980. code_editor->get_text_edit()->set_highlight_all_occurrences(EditorSettings::get_singleton()->get("text_editor/highlight_all_occurrences"));
  981. code_editor->get_text_edit()->cursor_set_blink_enabled(EditorSettings::get_singleton()->get("text_editor/caret_blink"));
  982. code_editor->get_text_edit()->cursor_set_blink_speed(EditorSettings::get_singleton()->get("text_editor/caret_blink_speed"));
  983. code_editor->get_text_edit()->set_draw_breakpoint_gutter(EditorSettings::get_singleton()->get("text_editor/show_breakpoint_gutter"));
  984. code_editor->get_text_edit()->cursor_set_block_mode(EditorSettings::get_singleton()->get("text_editor/block_caret"));
  985. code_editor->get_text_edit()->set_callhint_settings(
  986. EditorSettings::get_singleton()->get("text_editor/put_callhint_tooltip_below_current_line"),
  987. EditorSettings::get_singleton()->get("text_editor/callhint_tooltip_offset"));
  988. code_editor->get_text_edit()->set_select_identifiers_on_hover(true);
  989. code_editor->get_text_edit()->set_context_menu_enabled(false);
  990. code_editor->get_text_edit()->connect("input_event", this, "_text_edit_input_event");
  991. context_menu = memnew(PopupMenu);
  992. add_child(context_menu);
  993. context_menu->connect("item_pressed", this, "_edit_option");
  994. color_panel = memnew(PopupPanel);
  995. add_child(color_panel);
  996. color_picker = memnew(ColorPicker);
  997. color_panel->add_child(color_picker);
  998. color_panel->set_child_rect(color_picker);
  999. color_picker->connect("color_changed", this, "_color_changed");
  1000. edit_hb = memnew (HBoxContainer);
  1001. edit_menu = memnew( MenuButton );
  1002. edit_menu->set_text(TTR("Edit"));
  1003. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/undo"), EDIT_UNDO);
  1004. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/redo"), EDIT_REDO);
  1005. edit_menu->get_popup()->add_separator();
  1006. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/cut"), EDIT_CUT);
  1007. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/copy"), EDIT_COPY);
  1008. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/paste"), EDIT_PASTE);
  1009. edit_menu->get_popup()->add_separator();
  1010. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/select_all"), EDIT_SELECT_ALL);
  1011. edit_menu->get_popup()->add_separator();
  1012. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/move_up"), EDIT_MOVE_LINE_UP);
  1013. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/move_down"), EDIT_MOVE_LINE_DOWN);
  1014. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/indent_left"), EDIT_INDENT_LEFT);
  1015. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/indent_right"), EDIT_INDENT_RIGHT);
  1016. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/toggle_comment"), EDIT_TOGGLE_COMMENT);
  1017. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/clone_down"), EDIT_CLONE_DOWN);
  1018. edit_menu->get_popup()->add_separator();
  1019. #ifdef OSX_ENABLED
  1020. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/complete_symbol"), EDIT_COMPLETE);
  1021. #else
  1022. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/complete_symbol"), EDIT_COMPLETE);
  1023. #endif
  1024. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/trim_trailing_whitespace"), EDIT_TRIM_TRAILING_WHITESAPCE);
  1025. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/auto_indent"), EDIT_AUTO_INDENT);
  1026. edit_menu->get_popup()->connect("item_pressed", this,"_edit_option");
  1027. edit_menu->get_popup()->add_separator();
  1028. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/toggle_breakpoint"), DEBUG_TOGGLE_BREAKPOINT);
  1029. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/remove_all_breakpoints"), DEBUG_REMOVE_ALL_BREAKPOINTS);
  1030. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/goto_next_breakpoint"), DEBUG_GOTO_NEXT_BREAKPOINT);
  1031. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/goto_previous_breakpoint"), DEBUG_GOTO_PREV_BREAKPOINT);
  1032. search_menu = memnew( MenuButton );
  1033. edit_hb->add_child(search_menu);
  1034. search_menu->set_text(TTR("Search"));
  1035. search_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/find"), SEARCH_FIND);
  1036. search_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/find_next"), SEARCH_FIND_NEXT);
  1037. search_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/find_previous"), SEARCH_FIND_PREV);
  1038. search_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/replace"), SEARCH_REPLACE);
  1039. search_menu->get_popup()->add_separator();
  1040. search_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/goto_function"), SEARCH_LOCATE_FUNCTION);
  1041. search_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/goto_line"), SEARCH_GOTO_LINE);
  1042. search_menu->get_popup()->add_separator();
  1043. search_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/contextual_help"), HELP_CONTEXTUAL);
  1044. search_menu->get_popup()->connect("item_pressed", this,"_edit_option");
  1045. edit_hb->add_child(edit_menu);
  1046. quick_open = memnew( ScriptEditorQuickOpen );
  1047. add_child(quick_open);
  1048. quick_open->connect("goto_line",this,"_goto_line");
  1049. goto_line_dialog = memnew(GotoLineDialog);
  1050. add_child(goto_line_dialog);
  1051. code_editor->get_text_edit()->set_drag_forwarding(this);
  1052. }
  1053. static ScriptEditorBase * create_editor(const Ref<Script>& p_script) {
  1054. if (p_script->has_source_code()) {
  1055. return memnew( ScriptTextEditor );
  1056. }
  1057. return NULL;
  1058. }
  1059. void ScriptTextEditor::register_editor() {
  1060. ED_SHORTCUT("script_text_editor/undo", TTR("Undo"), KEY_MASK_CMD|KEY_Z);
  1061. ED_SHORTCUT("script_text_editor/redo", TTR("Redo"), KEY_MASK_CMD|KEY_Y);
  1062. ED_SHORTCUT("script_text_editor/cut", TTR("Cut"), KEY_MASK_CMD|KEY_X);
  1063. ED_SHORTCUT("script_text_editor/copy", TTR("Copy"), KEY_MASK_CMD|KEY_C);
  1064. ED_SHORTCUT("script_text_editor/paste", TTR("Paste"), KEY_MASK_CMD|KEY_V);
  1065. ED_SHORTCUT("script_text_editor/select_all", TTR("Select All"), KEY_MASK_CMD|KEY_A);
  1066. ED_SHORTCUT("script_text_editor/move_up", TTR("Move Up"), KEY_MASK_ALT|KEY_UP);
  1067. ED_SHORTCUT("script_text_editor/move_down", TTR("Move Down"), KEY_MASK_ALT|KEY_DOWN);
  1068. ED_SHORTCUT("script_text_editor/indent_left", TTR("Indent Left"), KEY_MASK_ALT|KEY_LEFT);
  1069. ED_SHORTCUT("script_text_editor/indent_right", TTR("Indent Right"), KEY_MASK_ALT|KEY_RIGHT);
  1070. ED_SHORTCUT("script_text_editor/toggle_comment", TTR("Toggle Comment"), KEY_MASK_CMD|KEY_K);
  1071. ED_SHORTCUT("script_text_editor/clone_down", TTR("Clone Down"), KEY_MASK_CMD|KEY_B);
  1072. #ifdef OSX_ENABLED
  1073. ED_SHORTCUT("script_text_editor/complete_symbol", TTR("Complete Symbol"), KEY_MASK_CTRL|KEY_SPACE);
  1074. #else
  1075. ED_SHORTCUT("script_text_editor/complete_symbol", TTR("Complete Symbol"), KEY_MASK_CMD|KEY_SPACE);
  1076. #endif
  1077. ED_SHORTCUT("script_text_editor/trim_trailing_whitespace", TTR("Trim Trailing Whitespace"), KEY_MASK_CTRL|KEY_MASK_ALT|KEY_T);
  1078. ED_SHORTCUT("script_text_editor/auto_indent", TTR("Auto Indent"), KEY_MASK_CMD|KEY_I);
  1079. ED_SHORTCUT("script_text_editor/toggle_breakpoint", TTR("Toggle Breakpoint"), KEY_F9);
  1080. ED_SHORTCUT("script_text_editor/remove_all_breakpoints", TTR("Remove All Breakpoints"), KEY_MASK_CTRL|KEY_MASK_SHIFT|KEY_F9);
  1081. ED_SHORTCUT("script_text_editor/goto_next_breakpoint", TTR("Goto Next Breakpoint"), KEY_MASK_CTRL|KEY_PERIOD);
  1082. ED_SHORTCUT("script_text_editor/goto_previous_breakpoint", TTR("Goto Previous Breakpoint"), KEY_MASK_CTRL|KEY_COMMA);
  1083. ED_SHORTCUT("script_text_editor/find", TTR("Find.."), KEY_MASK_CMD|KEY_F);
  1084. ED_SHORTCUT("script_text_editor/find_next", TTR("Find Next"), KEY_F3);
  1085. ED_SHORTCUT("script_text_editor/find_previous", TTR("Find Previous"), KEY_MASK_SHIFT|KEY_F3);
  1086. ED_SHORTCUT("script_text_editor/replace", TTR("Replace.."), KEY_MASK_CMD|KEY_R);
  1087. ED_SHORTCUT("script_text_editor/goto_function", TTR("Goto Function.."), KEY_MASK_SHIFT|KEY_MASK_CMD|KEY_F);
  1088. ED_SHORTCUT("script_text_editor/goto_line", TTR("Goto Line.."), KEY_MASK_CMD|KEY_L);
  1089. ED_SHORTCUT("script_text_editor/contextual_help", TTR("Contextual Help"), KEY_MASK_SHIFT|KEY_F1);
  1090. ScriptEditor::register_create_script_editor_function(create_editor);
  1091. }