code_editor.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594
  1. /*************************************************************************/
  2. /* code_editor.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2014 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 "code_editor.h"
  30. #include "editor_settings.h"
  31. #include "scene/gui/margin_container.h"
  32. #include "scene/gui/separator.h"
  33. void GotoLineDialog::popup_find_line(TextEdit *p_edit) {
  34. text_editor=p_edit;
  35. line->set_text(itos(text_editor->cursor_get_line()));
  36. line->select_all();
  37. popup_centered(Size2(180,80));
  38. line->grab_focus();
  39. }
  40. int GotoLineDialog::get_line() const {
  41. return line->get_text().to_int();
  42. }
  43. void GotoLineDialog::ok_pressed() {
  44. if (get_line()<1 || get_line()>text_editor->get_line_count())
  45. return;
  46. text_editor->cursor_set_line(get_line()-1);
  47. hide();
  48. }
  49. GotoLineDialog::GotoLineDialog() {
  50. set_title("Go to Line");
  51. Label *l = memnew(Label);
  52. l->set_text("Line Number:");
  53. l->set_pos(Point2(5,5));
  54. add_child(l);
  55. line = memnew( LineEdit );
  56. line->set_anchor( MARGIN_RIGHT, ANCHOR_END );
  57. line->set_begin( Point2(15,22) );
  58. line->set_end( Point2(15,35) );
  59. add_child(line);
  60. register_text_enter(line);
  61. text_editor=NULL;
  62. set_hide_on_ok(false);
  63. }
  64. void FindReplaceDialog::popup_search() {
  65. set_title("Search");
  66. replace_mc->hide();
  67. replace_label->hide();
  68. replace_vb->hide();
  69. skip->hide();
  70. popup_centered(Point2(300,190));
  71. get_ok()->set_text("Find");
  72. search_text->grab_focus();
  73. if (text_edit->is_selection_active() && ( text_edit->get_selection_from_line() == text_edit->get_selection_to_line())) {
  74. search_text->set_text( text_edit->get_selection_text() );
  75. }
  76. search_text->select_all();
  77. error_label->set_text("");
  78. }
  79. void FindReplaceDialog::popup_replace() {
  80. set_title("Replace");
  81. bool do_selection=(text_edit->is_selection_active() && text_edit->get_selection_from_line() < text_edit->get_selection_to_line());
  82. set_replace_selection_only(do_selection);
  83. replace_mc->show();
  84. replace_label->show();
  85. replace_vb->show();
  86. popup_centered(Point2(300,300));
  87. search_text->grab_focus();
  88. search_text->select_all();
  89. error_label->set_text("");
  90. if (prompt->is_pressed()) {
  91. skip->show();
  92. get_ok()->set_text("Next");
  93. selection_only->set_disabled(true);
  94. } else {
  95. skip->hide();
  96. get_ok()->set_text("Replace");
  97. selection_only->set_disabled(false);
  98. }
  99. }
  100. void FindReplaceDialog::_search_callback() {
  101. if (is_replace_mode())
  102. _replace();
  103. else
  104. _search();
  105. }
  106. void FindReplaceDialog::_replace_skip_callback() {
  107. _search();
  108. }
  109. void FindReplaceDialog::_replace() {
  110. if (is_replace_all_mode()) {
  111. //line as x so it gets priority in comparison, column as y
  112. Point2i orig_cursor(text_edit->cursor_get_line(),text_edit->cursor_get_column());
  113. Point2i prev_match=Point2(-1,-1);
  114. bool selection_enabled = text_edit->is_selection_active();
  115. Point2i selection_begin,selection_end;
  116. if (selection_enabled) {
  117. selection_begin=Point2i(text_edit->get_selection_from_line(),text_edit->get_selection_from_column());
  118. selection_end=Point2i(text_edit->get_selection_to_line(),text_edit->get_selection_to_column());
  119. }
  120. int vsval = text_edit->get_v_scroll();
  121. //int hsval = text_edit->get_h_scroll();
  122. text_edit->cursor_set_line(0);
  123. text_edit->cursor_set_column(0);
  124. int rc=0;
  125. while(_search()) {
  126. if (!text_edit->is_selection_active()) {
  127. //search selects
  128. break;
  129. }
  130. //replace area
  131. Point2i match_from(text_edit->get_selection_from_line(),text_edit->get_selection_from_column());
  132. Point2i match_to(text_edit->get_selection_to_line(),text_edit->get_selection_to_column());
  133. if (match_from < prev_match)
  134. break; //done
  135. prev_match=match_to;
  136. if (selection_enabled && is_replace_selection_only()) {
  137. if (match_from<selection_begin || match_to>selection_end)
  138. continue;
  139. //replace but adjust selection bounds
  140. text_edit->insert_text_at_cursor(get_replace_text());
  141. if (match_to.x==selection_end.x)
  142. selection_end.y+=get_replace_text().length() - get_search_text().length();
  143. } else {
  144. //just replace
  145. text_edit->insert_text_at_cursor(get_replace_text());
  146. }
  147. rc++;
  148. }
  149. //restore editor state (selection, cursor, scroll)
  150. text_edit->cursor_set_line(orig_cursor.x);
  151. text_edit->cursor_set_column(orig_cursor.y);
  152. if (selection_enabled && is_replace_selection_only()) {
  153. //reselect
  154. text_edit->select(selection_begin.x,selection_begin.y,selection_end.x,selection_end.y);
  155. } else {
  156. text_edit->deselect();
  157. }
  158. text_edit->set_v_scroll(vsval);
  159. // text_edit->set_h_scroll(hsval);
  160. error_label->set_text("Replaced "+itos(rc)+" ocurrence(s).");
  161. //hide();
  162. } else {
  163. if (text_edit->get_selection_text()==get_search_text()) {
  164. text_edit->insert_text_at_cursor(get_replace_text());
  165. }
  166. _search();
  167. }
  168. }
  169. bool FindReplaceDialog::_search() {
  170. String text=get_search_text();
  171. uint32_t flags=0;
  172. if (is_whole_words())
  173. flags|=TextEdit::SEARCH_WHOLE_WORDS;
  174. if (is_case_sensitive())
  175. flags|=TextEdit::SEARCH_MATCH_CASE;
  176. if (is_backwards())
  177. flags|=TextEdit::SEARCH_BACKWARDS;
  178. int line,col;
  179. bool found = text_edit->search(text,flags,text_edit->cursor_get_line(),text_edit->cursor_get_column(),line,col);
  180. if (found) {
  181. print_line("found");
  182. text_edit->cursor_set_line(line);
  183. text_edit->cursor_set_column(col+text.length());
  184. text_edit->select(line,col,line,col+text.length());
  185. set_error("");
  186. return true;
  187. } else {
  188. set_error("Not Found!");
  189. return false;
  190. }
  191. }
  192. void FindReplaceDialog::_prompt_changed() {
  193. if (prompt->is_pressed()) {
  194. skip->show();
  195. get_ok()->set_text("Next");
  196. selection_only->set_disabled(true);
  197. } else {
  198. skip->hide();
  199. get_ok()->set_text("Replace");
  200. selection_only->set_disabled(false);
  201. }
  202. }
  203. void FindReplaceDialog::_skip_pressed() {
  204. _replace_skip_callback();
  205. }
  206. bool FindReplaceDialog::is_replace_mode() const {
  207. return replace_text->is_visible();
  208. }
  209. bool FindReplaceDialog::is_replace_all_mode() const {
  210. return !prompt->is_pressed();
  211. }
  212. bool FindReplaceDialog::is_replace_selection_only() const {
  213. return selection_only->is_pressed();
  214. }
  215. void FindReplaceDialog::set_replace_selection_only(bool p_enable){
  216. selection_only->set_pressed(p_enable);
  217. }
  218. void FindReplaceDialog::ok_pressed() {
  219. _search_callback();
  220. }
  221. void FindReplaceDialog::_search_text_entered(const String& p_text) {
  222. if (replace_text->is_visible())
  223. return;
  224. emit_signal("search");
  225. _search();
  226. }
  227. void FindReplaceDialog::_replace_text_entered(const String& p_text) {
  228. if (!replace_text->is_visible())
  229. return;
  230. emit_signal("search");
  231. _replace();
  232. }
  233. String FindReplaceDialog::get_search_text() const {
  234. return search_text->get_text();
  235. }
  236. String FindReplaceDialog::get_replace_text() const {
  237. return replace_text->get_text();
  238. }
  239. bool FindReplaceDialog::is_whole_words() const {
  240. return whole_words->is_pressed();
  241. }
  242. bool FindReplaceDialog::is_case_sensitive() const {
  243. return case_sensitive->is_pressed();
  244. }
  245. bool FindReplaceDialog::is_backwards() const {
  246. return backwards->is_pressed();
  247. }
  248. void FindReplaceDialog::set_error(const String& p_error) {
  249. error_label->set_text(p_error);
  250. }
  251. void FindReplaceDialog::set_text_edit(TextEdit *p_text_edit) {
  252. text_edit=p_text_edit;
  253. }
  254. void FindReplaceDialog::search_next() {
  255. _search();
  256. }
  257. void FindReplaceDialog::_bind_methods() {
  258. ObjectTypeDB::bind_method("_search_text_entered",&FindReplaceDialog::_search_text_entered);
  259. ObjectTypeDB::bind_method("_replace_text_entered",&FindReplaceDialog::_replace_text_entered);
  260. ObjectTypeDB::bind_method("_prompt_changed",&FindReplaceDialog::_prompt_changed);
  261. ObjectTypeDB::bind_method("_skip_pressed",&FindReplaceDialog::_skip_pressed);
  262. ADD_SIGNAL(MethodInfo("search"));
  263. ADD_SIGNAL(MethodInfo("skip"));
  264. }
  265. FindReplaceDialog::FindReplaceDialog() {
  266. set_self_opacity(0.6);
  267. VBoxContainer *vb = memnew( VBoxContainer );
  268. add_child(vb);
  269. set_child_rect(vb);
  270. search_text = memnew( LineEdit );
  271. vb->add_margin_child("Search",search_text);
  272. search_text->connect("text_entered", this,"_search_text_entered");
  273. search_text->set_self_opacity(0.7);
  274. replace_label = memnew( Label);
  275. replace_label->set_text("Replace By");
  276. vb->add_child(replace_label);
  277. replace_mc= memnew( MarginContainer);
  278. vb->add_child(replace_mc);
  279. replace_text = memnew( LineEdit );
  280. replace_text->set_anchor( MARGIN_RIGHT, ANCHOR_END );
  281. replace_text->set_begin( Point2(15,132) );
  282. replace_text->set_end( Point2(15,135) );
  283. replace_text->set_self_opacity(0.7);
  284. replace_mc->add_child(replace_text);
  285. replace_text->connect("text_entered", this,"_replace_text_entered");
  286. MarginContainer *opt_mg = memnew( MarginContainer );
  287. vb->add_child(opt_mg);
  288. VBoxContainer *svb = memnew( VBoxContainer);
  289. opt_mg->add_child(svb);
  290. svb ->add_child(memnew(Label));
  291. whole_words = memnew( CheckButton );
  292. whole_words->set_text("Whole Words");
  293. svb->add_child(whole_words);
  294. case_sensitive = memnew( CheckButton );
  295. case_sensitive->set_text("Case Sensitive");
  296. svb->add_child(case_sensitive);
  297. backwards = memnew( CheckButton );
  298. backwards->set_text("Backwards");
  299. svb->add_child(backwards);
  300. opt_mg = memnew( MarginContainer );
  301. vb->add_child(opt_mg);
  302. VBoxContainer *rvb = memnew( VBoxContainer);
  303. opt_mg->add_child(rvb);
  304. replace_vb=rvb;
  305. // rvb ->add_child(memnew(HSeparator));
  306. rvb ->add_child(memnew(Label));
  307. prompt = memnew( CheckButton );
  308. prompt->set_text("Prompt On Replace");
  309. rvb->add_child(prompt);
  310. prompt->connect("pressed", this,"_prompt_changed");
  311. selection_only = memnew( CheckButton );
  312. selection_only->set_text("Selection Only");
  313. rvb->add_child(selection_only);
  314. int margin = get_constant("margin","Dialogs");
  315. int button_margin = get_constant("button_margin","Dialogs");
  316. skip = memnew( Button );
  317. skip->set_anchor( MARGIN_LEFT, ANCHOR_END );
  318. skip->set_anchor( MARGIN_TOP, ANCHOR_END );
  319. skip->set_anchor( MARGIN_RIGHT, ANCHOR_END );
  320. skip->set_anchor( MARGIN_BOTTOM, ANCHOR_END );
  321. skip->set_begin( Point2( 70, button_margin ) );
  322. skip->set_end( Point2( 10, margin ) );
  323. skip->set_text("Skip");
  324. add_child(skip);
  325. skip->connect("pressed", this,"_skip_pressed");
  326. error_label = memnew( Label );
  327. error_label->set_align(Label::ALIGN_CENTER);
  328. error_label->add_color_override("font_color",Color(1,0.4,0.3));
  329. error_label->add_color_override("font_color_shadow",Color(0,0,0,0.2));
  330. error_label->add_constant_override("shadow_as_outline",1);
  331. vb->add_child(error_label);
  332. set_hide_on_ok(false);
  333. }
  334. /*** CODE EDITOR ****/
  335. void CodeTextEditor::_line_col_changed() {
  336. String text = String()+"Line: "+itos(text_editor->cursor_get_line()+1)+", Col: "+itos(text_editor->cursor_get_column());
  337. line_col->set_text(text);
  338. }
  339. void CodeTextEditor::_text_changed() {
  340. idle->start();
  341. }
  342. void CodeTextEditor::_complete_request(const String& p_request, int p_line) {
  343. List<String> entries;
  344. _code_complete_script(text_editor->get_text(),p_request,p_line,&entries);
  345. print_line("COMPLETE: "+p_request);
  346. Vector<String> strs;
  347. strs.resize(entries.size());
  348. int i=0;
  349. for(List<String>::Element *E=entries.front();E;E=E->next()) {
  350. strs[i++]=E->get();
  351. }
  352. text_editor->code_complete(strs);
  353. }
  354. void CodeTextEditor::set_error(const String& p_error) {
  355. if (p_error!="") {
  356. error->set_text(p_error);
  357. error->show();
  358. } else {
  359. error->hide();
  360. }
  361. }
  362. void CodeTextEditor::_on_settings_change() {
  363. String editor_font = EditorSettings::get_singleton()->get("text_editor/font");
  364. if (editor_font!="") {
  365. Ref<Font> fnt = ResourceLoader::load(editor_font);
  366. if (fnt.is_valid()) {
  367. text_editor->add_font_override("font",fnt);
  368. return;
  369. }
  370. }
  371. text_editor->add_font_override("font",get_font("source","Fonts"));
  372. text_editor->set_auto_brace_completion(
  373. EDITOR_DEF("text_editor/auto_brace_complete", false)
  374. );
  375. }
  376. void CodeTextEditor::_text_changed_idle_timeout() {
  377. _validate_script();
  378. }
  379. void CodeTextEditor::_notification(int p_what) {
  380. if (p_what==EditorSettings::NOTIFICATION_EDITOR_SETTINGS_CHANGED)
  381. _load_theme_settings();
  382. }
  383. void CodeTextEditor::_bind_methods() {
  384. ObjectTypeDB::bind_method("_line_col_changed",&CodeTextEditor::_line_col_changed);
  385. ObjectTypeDB::bind_method("_text_changed",&CodeTextEditor::_text_changed);
  386. ObjectTypeDB::bind_method("_on_settings_change",&CodeTextEditor::_on_settings_change);
  387. ObjectTypeDB::bind_method("_text_changed_idle_timeout",&CodeTextEditor::_text_changed_idle_timeout);
  388. ObjectTypeDB::bind_method("_complete_request",&CodeTextEditor::_complete_request);
  389. }
  390. CodeTextEditor::CodeTextEditor() {
  391. text_editor = memnew( TextEdit );
  392. add_child(text_editor);
  393. text_editor->set_area_as_parent_rect();
  394. text_editor->set_margin(MARGIN_BOTTOM,20);
  395. text_editor->add_font_override("font",get_font("source","Fonts"));
  396. text_editor->set_show_line_numbers(true);
  397. line_col = memnew( Label );
  398. add_child(line_col);
  399. line_col->set_anchor_and_margin(MARGIN_LEFT,ANCHOR_END,135);
  400. line_col->set_anchor_and_margin(MARGIN_TOP,ANCHOR_END,20);
  401. line_col->set_anchor_and_margin(MARGIN_BOTTOM,ANCHOR_END,1);
  402. line_col->set_anchor_and_margin(MARGIN_RIGHT,ANCHOR_END,5);
  403. //line_col->set_align(Label::ALIGN_RIGHT);
  404. idle = memnew( Timer );
  405. add_child(idle);
  406. idle->set_one_shot(true);
  407. idle->set_wait_time(EDITOR_DEF("text_editor/idle_parse_delay",2));
  408. error = memnew( Label );
  409. add_child(error);
  410. error->set_anchor_and_margin(MARGIN_LEFT,ANCHOR_BEGIN,5);
  411. error->set_anchor_and_margin(MARGIN_TOP,ANCHOR_END,20);
  412. error->set_anchor_and_margin(MARGIN_BOTTOM,ANCHOR_END,1);
  413. error->set_anchor_and_margin(MARGIN_RIGHT,ANCHOR_END,130);
  414. error->hide();
  415. error->add_color_override("font_color",Color(1,0.7,0.6,0.9));
  416. text_editor->connect("cursor_changed", this,"_line_col_changed");
  417. text_editor->connect("text_changed", this,"_text_changed");
  418. text_editor->connect("request_completion", this,"_complete_request");
  419. Vector<String> cs;
  420. cs.push_back(".");
  421. text_editor->set_completion(true,cs);
  422. idle->connect("timeout", this,"_text_changed_idle_timeout");
  423. EditorSettings::get_singleton()->connect("settings_changed",this,"_on_settings_change");
  424. }