script_text_editor.cpp 42 KB

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