code_editor.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641
  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=text_edit->cursor_get_line(),col=text_edit->cursor_get_column();
  187. if (is_backwards()) {
  188. col-=1;
  189. if (col<0) {
  190. line-=1;
  191. if (line<0) {
  192. line=text_edit->get_line_count()-1;
  193. }
  194. col=text_edit->get_line(line).length();
  195. }
  196. }
  197. bool found = text_edit->search(text,flags,line,col,line,col);
  198. if (found) {
  199. // print_line("found");
  200. text_edit->cursor_set_line(line);
  201. if (is_backwards())
  202. text_edit->cursor_set_column(col);
  203. else
  204. text_edit->cursor_set_column(col+text.length());
  205. text_edit->select(line,col,line,col+text.length());
  206. set_error("");
  207. return true;
  208. } else {
  209. set_error("Not Found!");
  210. return false;
  211. }
  212. }
  213. void FindReplaceDialog::_prompt_changed() {
  214. if (prompt->is_pressed()) {
  215. skip->show();
  216. get_ok()->set_text("Next");
  217. selection_only->set_disabled(true);
  218. } else {
  219. skip->hide();
  220. get_ok()->set_text("Replace");
  221. selection_only->set_disabled(false);
  222. }
  223. }
  224. void FindReplaceDialog::_skip_pressed() {
  225. _replace_skip_callback();
  226. }
  227. bool FindReplaceDialog::is_replace_mode() const {
  228. return replace_text->is_visible();
  229. }
  230. bool FindReplaceDialog::is_replace_all_mode() const {
  231. return !prompt->is_pressed();
  232. }
  233. bool FindReplaceDialog::is_replace_selection_only() const {
  234. return selection_only->is_pressed();
  235. }
  236. void FindReplaceDialog::set_replace_selection_only(bool p_enable){
  237. selection_only->set_pressed(p_enable);
  238. }
  239. void FindReplaceDialog::ok_pressed() {
  240. _search_callback();
  241. }
  242. void FindReplaceDialog::_search_text_entered(const String& p_text) {
  243. if (replace_text->is_visible())
  244. return;
  245. emit_signal("search");
  246. _search();
  247. }
  248. void FindReplaceDialog::_replace_text_entered(const String& p_text) {
  249. if (!replace_text->is_visible())
  250. return;
  251. emit_signal("search");
  252. _replace();
  253. }
  254. String FindReplaceDialog::get_search_text() const {
  255. return search_text->get_text();
  256. }
  257. String FindReplaceDialog::get_replace_text() const {
  258. return replace_text->get_text();
  259. }
  260. bool FindReplaceDialog::is_whole_words() const {
  261. return whole_words->is_pressed();
  262. }
  263. bool FindReplaceDialog::is_case_sensitive() const {
  264. return case_sensitive->is_pressed();
  265. }
  266. bool FindReplaceDialog::is_backwards() const {
  267. return backwards->is_pressed();
  268. }
  269. void FindReplaceDialog::set_error(const String& p_error) {
  270. error_label->set_text(p_error);
  271. }
  272. void FindReplaceDialog::set_text_edit(TextEdit *p_text_edit) {
  273. text_edit=p_text_edit;
  274. }
  275. void FindReplaceDialog::search_next() {
  276. _search();
  277. }
  278. void FindReplaceDialog::_bind_methods() {
  279. ObjectTypeDB::bind_method("_search_text_entered",&FindReplaceDialog::_search_text_entered);
  280. ObjectTypeDB::bind_method("_replace_text_entered",&FindReplaceDialog::_replace_text_entered);
  281. ObjectTypeDB::bind_method("_prompt_changed",&FindReplaceDialog::_prompt_changed);
  282. ObjectTypeDB::bind_method("_skip_pressed",&FindReplaceDialog::_skip_pressed);
  283. ADD_SIGNAL(MethodInfo("search"));
  284. ADD_SIGNAL(MethodInfo("skip"));
  285. }
  286. FindReplaceDialog::FindReplaceDialog() {
  287. set_self_opacity(0.8);
  288. VBoxContainer *vb = memnew( VBoxContainer );
  289. add_child(vb);
  290. set_child_rect(vb);
  291. search_text = memnew( LineEdit );
  292. vb->add_margin_child("Search",search_text);
  293. search_text->connect("text_entered", this,"_search_text_entered");
  294. //search_text->set_self_opacity(0.7);
  295. replace_label = memnew( Label);
  296. replace_label->set_text("Replace By");
  297. vb->add_child(replace_label);
  298. replace_mc= memnew( MarginContainer);
  299. vb->add_child(replace_mc);
  300. replace_text = memnew( LineEdit );
  301. replace_text->set_anchor( MARGIN_RIGHT, ANCHOR_END );
  302. replace_text->set_begin( Point2(15,132) );
  303. replace_text->set_end( Point2(15,135) );
  304. //replace_text->set_self_opacity(0.7);
  305. replace_mc->add_child(replace_text);
  306. replace_text->connect("text_entered", this,"_replace_text_entered");
  307. MarginContainer *opt_mg = memnew( MarginContainer );
  308. vb->add_child(opt_mg);
  309. VBoxContainer *svb = memnew( VBoxContainer);
  310. opt_mg->add_child(svb);
  311. svb ->add_child(memnew(Label));
  312. whole_words = memnew( CheckButton );
  313. whole_words->set_text("Whole Words");
  314. svb->add_child(whole_words);
  315. case_sensitive = memnew( CheckButton );
  316. case_sensitive->set_text("Case Sensitive");
  317. svb->add_child(case_sensitive);
  318. backwards = memnew( CheckButton );
  319. backwards->set_text("Backwards");
  320. svb->add_child(backwards);
  321. opt_mg = memnew( MarginContainer );
  322. vb->add_child(opt_mg);
  323. VBoxContainer *rvb = memnew( VBoxContainer);
  324. opt_mg->add_child(rvb);
  325. replace_vb=rvb;
  326. // rvb ->add_child(memnew(HSeparator));
  327. rvb ->add_child(memnew(Label));
  328. prompt = memnew( CheckButton );
  329. prompt->set_text("Prompt On Replace");
  330. rvb->add_child(prompt);
  331. prompt->connect("pressed", this,"_prompt_changed");
  332. selection_only = memnew( CheckButton );
  333. selection_only->set_text("Selection Only");
  334. rvb->add_child(selection_only);
  335. int margin = get_constant("margin","Dialogs");
  336. int button_margin = get_constant("button_margin","Dialogs");
  337. skip = memnew( Button );
  338. skip->set_anchor( MARGIN_LEFT, ANCHOR_END );
  339. skip->set_anchor( MARGIN_TOP, ANCHOR_END );
  340. skip->set_anchor( MARGIN_RIGHT, ANCHOR_END );
  341. skip->set_anchor( MARGIN_BOTTOM, ANCHOR_END );
  342. skip->set_begin( Point2( 70, button_margin ) );
  343. skip->set_end( Point2( 10, margin ) );
  344. skip->set_text("Skip");
  345. add_child(skip);
  346. skip->connect("pressed", this,"_skip_pressed");
  347. error_label = memnew( Label );
  348. error_label->set_align(Label::ALIGN_CENTER);
  349. error_label->add_color_override("font_color",Color(1,0.4,0.3));
  350. error_label->add_color_override("font_color_shadow",Color(0,0,0,0.2));
  351. error_label->add_constant_override("shadow_as_outline",1);
  352. vb->add_child(error_label);
  353. set_hide_on_ok(false);
  354. }
  355. /*** CODE EDITOR ****/
  356. void CodeTextEditor::_line_col_changed() {
  357. String text = String()+"Line: "+itos(text_editor->cursor_get_line()+1)+", Col: "+itos(text_editor->cursor_get_column());
  358. line_col->set_text(text);
  359. }
  360. void CodeTextEditor::_text_changed() {
  361. code_complete_timer->start();
  362. idle->start();
  363. }
  364. void CodeTextEditor::_code_complete_timer_timeout() {
  365. if (enable_complete_timer)
  366. text_editor->query_code_comple();
  367. }
  368. void CodeTextEditor::_complete_request(const String& p_request, int p_line) {
  369. List<String> entries;
  370. _code_complete_script(text_editor->get_text(),p_request,p_line,&entries);
  371. // print_line("COMPLETE: "+p_request);
  372. Vector<String> strs;
  373. strs.resize(entries.size());
  374. int i=0;
  375. for(List<String>::Element *E=entries.front();E;E=E->next()) {
  376. strs[i++]=E->get();
  377. }
  378. text_editor->code_complete(strs);
  379. }
  380. void CodeTextEditor::set_error(const String& p_error) {
  381. if (p_error!="") {
  382. error->set_text(p_error);
  383. error->show();
  384. } else {
  385. error->hide();
  386. }
  387. }
  388. void CodeTextEditor::_on_settings_change() {
  389. // FONTS
  390. String editor_font = EDITOR_DEF("text_editor/font", "");
  391. bool font_overrode = false;
  392. if (editor_font!="") {
  393. Ref<Font> fnt = ResourceLoader::load(editor_font);
  394. if (fnt.is_valid()) {
  395. text_editor->add_font_override("font",fnt);
  396. font_overrode = true;
  397. }
  398. }
  399. if(!font_overrode)
  400. text_editor->add_font_override("font",get_font("source","Fonts"));
  401. // AUTO BRACE COMPLETION
  402. text_editor->set_auto_brace_completion(
  403. EDITOR_DEF("text_editor/auto_brace_complete", false)
  404. );
  405. code_complete_timer->set_wait_time(
  406. EDITOR_DEF("text_editor/code_complete_delay",.3f)
  407. );
  408. enable_complete_timer = EDITOR_DEF("text_editor/enable_code_completion_delay",true);
  409. }
  410. void CodeTextEditor::_text_changed_idle_timeout() {
  411. _validate_script();
  412. }
  413. void CodeTextEditor::_notification(int p_what) {
  414. if (p_what==EditorSettings::NOTIFICATION_EDITOR_SETTINGS_CHANGED)
  415. _load_theme_settings();
  416. }
  417. void CodeTextEditor::_bind_methods() {
  418. ObjectTypeDB::bind_method("_line_col_changed",&CodeTextEditor::_line_col_changed);
  419. ObjectTypeDB::bind_method("_text_changed",&CodeTextEditor::_text_changed);
  420. ObjectTypeDB::bind_method("_on_settings_change",&CodeTextEditor::_on_settings_change);
  421. ObjectTypeDB::bind_method("_text_changed_idle_timeout",&CodeTextEditor::_text_changed_idle_timeout);
  422. ObjectTypeDB::bind_method("_code_complete_timer_timeout",&CodeTextEditor::_code_complete_timer_timeout);
  423. ObjectTypeDB::bind_method("_complete_request",&CodeTextEditor::_complete_request);
  424. }
  425. CodeTextEditor::CodeTextEditor() {
  426. text_editor = memnew( TextEdit );
  427. add_child(text_editor);
  428. text_editor->set_area_as_parent_rect();
  429. text_editor->set_margin(MARGIN_BOTTOM,20);
  430. text_editor->add_font_override("font",get_font("source","Fonts"));
  431. text_editor->set_show_line_numbers(true);
  432. line_col = memnew( Label );
  433. add_child(line_col);
  434. line_col->set_anchor_and_margin(MARGIN_LEFT,ANCHOR_END,135);
  435. line_col->set_anchor_and_margin(MARGIN_TOP,ANCHOR_END,20);
  436. line_col->set_anchor_and_margin(MARGIN_BOTTOM,ANCHOR_END,1);
  437. line_col->set_anchor_and_margin(MARGIN_RIGHT,ANCHOR_END,5);
  438. //line_col->set_align(Label::ALIGN_RIGHT);
  439. idle = memnew( Timer );
  440. add_child(idle);
  441. idle->set_one_shot(true);
  442. idle->set_wait_time(EDITOR_DEF("text_editor/idle_parse_delay",2));
  443. code_complete_timer = memnew(Timer);
  444. add_child(code_complete_timer);
  445. code_complete_timer->set_one_shot(true);
  446. enable_complete_timer = EDITOR_DEF("text_editor/enable_code_completion_delay",true);
  447. code_complete_timer->set_wait_time(EDITOR_DEF("text_editor/code_complete_delay",.3f));
  448. error = memnew( Label );
  449. add_child(error);
  450. error->set_anchor_and_margin(MARGIN_LEFT,ANCHOR_BEGIN,5);
  451. error->set_anchor_and_margin(MARGIN_TOP,ANCHOR_END,20);
  452. error->set_anchor_and_margin(MARGIN_BOTTOM,ANCHOR_END,1);
  453. error->set_anchor_and_margin(MARGIN_RIGHT,ANCHOR_END,130);
  454. error->hide();
  455. error->add_color_override("font_color",Color(1,0.7,0.6,0.9));
  456. text_editor->connect("cursor_changed", this,"_line_col_changed");
  457. text_editor->connect("text_changed", this,"_text_changed");
  458. text_editor->connect("request_completion", this,"_complete_request");
  459. Vector<String> cs;
  460. cs.push_back(".");
  461. text_editor->set_completion(true,cs);
  462. idle->connect("timeout", this,"_text_changed_idle_timeout");
  463. code_complete_timer->connect("timeout", this,"_code_complete_timer_timeout");
  464. EditorSettings::get_singleton()->connect("settings_changed",this,"_on_settings_change");
  465. }