code_editor.cpp 18 KB

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