script_text_editor.cpp 44 KB

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