code_editor.cpp 17 KB

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