code_editor.cpp 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155
  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. #include "os/keyboard.h"
  34. void GotoLineDialog::popup_find_line(TextEdit *p_edit) {
  35. text_editor=p_edit;
  36. line->set_text(itos(text_editor->cursor_get_line()));
  37. line->select_all();
  38. popup_centered(Size2(180,80));
  39. line->grab_focus();
  40. }
  41. int GotoLineDialog::get_line() const {
  42. return line->get_text().to_int();
  43. }
  44. void GotoLineDialog::ok_pressed() {
  45. if (get_line()<1 || get_line()>text_editor->get_line_count())
  46. return;
  47. text_editor->cursor_set_line(get_line()-1);
  48. hide();
  49. }
  50. GotoLineDialog::GotoLineDialog() {
  51. set_title(TTR("Go to Line"));
  52. Label *l = memnew(Label);
  53. l->set_text(TTR("Line Number:"));
  54. l->set_pos(Point2(5,5));
  55. add_child(l);
  56. line = memnew( LineEdit );
  57. line->set_anchor( MARGIN_RIGHT, ANCHOR_END );
  58. line->set_begin( Point2(15,22) );
  59. line->set_end( Point2(15,35) );
  60. add_child(line);
  61. register_text_enter(line);
  62. text_editor=NULL;
  63. set_hide_on_ok(false);
  64. }
  65. void FindReplaceBar::_notification(int p_what) {
  66. if (p_what == NOTIFICATION_READY) {
  67. find_prev->set_icon(get_icon("MoveUp", "EditorIcons"));
  68. find_next->set_icon(get_icon("MoveDown", "EditorIcons"));
  69. hide_button->set_normal_texture(get_icon("Close","EditorIcons"));
  70. hide_button->set_hover_texture(get_icon("CloseHover","EditorIcons"));
  71. hide_button->set_pressed_texture(get_icon("Close","EditorIcons"));
  72. } else if (p_what == NOTIFICATION_VISIBILITY_CHANGED) {
  73. set_process_unhandled_input(is_visible());
  74. }
  75. }
  76. void FindReplaceBar::_unhandled_input(const InputEvent &p_event) {
  77. if (p_event.type == InputEvent::KEY) {
  78. const InputEventKey& k = p_event.key;
  79. if (k.pressed && (text_edit->has_focus() || text_vbc->is_a_parent_of(get_focus_owner()))) {
  80. bool accepted = true;
  81. switch (k.scancode) {
  82. case KEY_ESCAPE: {
  83. _hide_bar();
  84. } break;
  85. default: {
  86. accepted = false;
  87. } break;
  88. }
  89. if (accepted) {
  90. accept_event();
  91. }
  92. }
  93. }
  94. }
  95. bool FindReplaceBar::_search(uint32_t p_flags, int p_from_line, int p_from_col) {
  96. int line, col;
  97. String text=get_search_text();
  98. bool found=text_edit->search(text,p_flags,p_from_line,p_from_col,line,col);
  99. if (found) {
  100. if (!preserve_cursor) {
  101. text_edit->cursor_set_line(line);
  102. text_edit->cursor_set_column(col+text.length());
  103. }
  104. text_edit->set_search_text(text);
  105. text_edit->set_search_flags(p_flags);
  106. text_edit->set_current_search_result(line,col);
  107. result_line=line;
  108. result_col=col;
  109. set_error("");
  110. } else {
  111. result_line=-1;
  112. result_col=-1;
  113. text_edit->set_search_text("");
  114. set_error(text.empty()?"":TTR("No Matches"));
  115. }
  116. return found;
  117. }
  118. void FindReplaceBar::_replace() {
  119. if (result_line!=-1 && result_col!=-1) {
  120. text_edit->begin_complex_operation();
  121. text_edit->select(result_line,result_col,result_line,result_col+get_search_text().length());
  122. text_edit->insert_text_at_cursor(get_replace_text());
  123. text_edit->end_complex_operation();
  124. }
  125. search_current();
  126. }
  127. void FindReplaceBar::_replace_all() {
  128. // line as x so it gets priority in comparison, column as y
  129. Point2i orig_cursor(text_edit->cursor_get_line(),text_edit->cursor_get_column());
  130. Point2i prev_match=Point2(-1,-1);
  131. bool selection_enabled = text_edit->is_selection_active();
  132. Point2i selection_begin,selection_end;
  133. if (selection_enabled) {
  134. selection_begin=Point2i(text_edit->get_selection_from_line(),text_edit->get_selection_from_column());
  135. selection_end=Point2i(text_edit->get_selection_to_line(),text_edit->get_selection_to_column());
  136. }
  137. int vsval = text_edit->get_v_scroll();
  138. text_edit->cursor_set_line(0);
  139. text_edit->cursor_set_column(0);
  140. int search_text_len=get_search_text().length();
  141. int rc=0;
  142. replace_all_mode = true;
  143. text_edit->begin_complex_operation();
  144. while (search_next()) {
  145. // replace area
  146. Point2i match_from(result_line,result_col);
  147. Point2i match_to(result_line,result_col+search_text_len);
  148. if (match_from < prev_match)
  149. break; // done
  150. prev_match=match_to;
  151. text_edit->select(result_line,result_col,result_line,match_to.y);
  152. if (selection_enabled && is_selection_only()) {
  153. if (match_from<selection_begin || match_to>selection_end)
  154. continue;
  155. // replace but adjust selection bounds
  156. text_edit->insert_text_at_cursor(get_replace_text());
  157. if (match_to.x==selection_end.x)
  158. selection_end.y+=get_replace_text().length() - get_search_text().length();
  159. } else {
  160. // just replace
  161. text_edit->insert_text_at_cursor(get_replace_text());
  162. }
  163. rc++;
  164. }
  165. text_edit->end_complex_operation();
  166. replace_all_mode = false;
  167. // restore editor state (selection, cursor, scroll)
  168. text_edit->cursor_set_line(orig_cursor.x);
  169. text_edit->cursor_set_column(orig_cursor.y);
  170. if (selection_enabled && is_selection_only()) {
  171. // reselect
  172. text_edit->select(selection_begin.x,selection_begin.y,selection_end.x,selection_end.y);
  173. } else {
  174. text_edit->deselect();
  175. }
  176. text_edit->set_v_scroll(vsval);
  177. set_error(vformat(TTR("Replaced %d Ocurrence(s)."), rc));
  178. }
  179. void FindReplaceBar::_get_search_from(int& r_line, int& r_col) {
  180. r_line=text_edit->cursor_get_line();
  181. r_col=text_edit->cursor_get_column();
  182. if (text_edit->is_selection_active() && !replace_all_mode) {
  183. int selection_line=text_edit->get_selection_from_line();
  184. if (text_edit->get_selection_text()==get_search_text() && r_line==selection_line) {
  185. int selection_from_col=text_edit->get_selection_from_column();
  186. if (r_col>=selection_from_col && r_col<=text_edit->get_selection_to_column()) {
  187. r_col=selection_line;
  188. r_col=selection_from_col;
  189. }
  190. }
  191. }
  192. if (r_line==result_line && r_col>=result_col && r_col<=result_col+get_search_text().length()) {
  193. r_col=result_col;
  194. }
  195. }
  196. bool FindReplaceBar::search_current() {
  197. uint32_t flags=0;
  198. if (is_whole_words())
  199. flags|=TextEdit::SEARCH_WHOLE_WORDS;
  200. if (is_case_sensitive())
  201. flags|=TextEdit::SEARCH_MATCH_CASE;
  202. int line, col;
  203. _get_search_from(line, col);
  204. return _search(flags,line,col);
  205. }
  206. bool FindReplaceBar::search_prev() {
  207. uint32_t flags=0;
  208. String text = get_search_text();
  209. if (is_whole_words())
  210. flags|=TextEdit::SEARCH_WHOLE_WORDS;
  211. if (is_case_sensitive())
  212. flags|=TextEdit::SEARCH_MATCH_CASE;
  213. flags|=TextEdit::SEARCH_BACKWARDS;
  214. int line, col;
  215. _get_search_from(line, col);
  216. col-=text.length();
  217. if (col<0) {
  218. line-=1;
  219. if (line<0)
  220. line=text_edit->get_line_count()-1;
  221. col=text_edit->get_line(line).length();
  222. }
  223. return _search(flags,line,col);
  224. }
  225. bool FindReplaceBar::search_next() {
  226. uint32_t flags=0;
  227. String text = get_search_text();
  228. if (is_whole_words())
  229. flags|=TextEdit::SEARCH_WHOLE_WORDS;
  230. if (is_case_sensitive())
  231. flags|=TextEdit::SEARCH_MATCH_CASE;
  232. int line, col;
  233. _get_search_from(line, col);
  234. if (line==result_line && col==result_col) {
  235. col+=text.length();
  236. if (col>text_edit->get_line(line).length()) {
  237. line+=1;
  238. if (line>=text_edit->get_line_count())
  239. line=0;
  240. col=0;
  241. }
  242. }
  243. return _search(flags,line,col);
  244. }
  245. void FindReplaceBar::_hide_bar() {
  246. if (replace_text->has_focus() || search_text->has_focus())
  247. text_edit->grab_focus();
  248. text_edit->set_search_text("");
  249. result_line = -1;
  250. result_col = -1;
  251. replace_hbc->hide();
  252. replace_options_hbc->hide();
  253. hide();
  254. }
  255. void FindReplaceBar::_show_search() {
  256. show();
  257. search_text->grab_focus();
  258. if (text_edit->is_selection_active()) {
  259. search_text->set_text(text_edit->get_selection_text());
  260. }
  261. if (!get_search_text().empty()) {
  262. search_text->select_all();
  263. search_text->set_cursor_pos(search_text->get_text().length());
  264. search_current();
  265. }
  266. }
  267. void FindReplaceBar::popup_search() {
  268. replace_hbc->hide();
  269. replace_options_hbc->hide();
  270. _show_search();
  271. }
  272. void FindReplaceBar::popup_replace() {
  273. if (!replace_hbc->is_visible() || !replace_options_hbc->is_visible()) {
  274. replace_text->clear();
  275. replace_hbc->show();
  276. replace_options_hbc->show();
  277. }
  278. _show_search();
  279. }
  280. void FindReplaceBar::_search_options_changed(bool p_pressed) {
  281. search_current();
  282. }
  283. void FindReplaceBar::_editor_text_changed() {
  284. if (is_visible()) {
  285. preserve_cursor=true;
  286. search_current();
  287. preserve_cursor=false;
  288. }
  289. }
  290. void FindReplaceBar::_search_text_changed(const String& p_text) {
  291. search_current();
  292. }
  293. void FindReplaceBar::_search_text_entered(const String& p_text) {
  294. search_next();
  295. }
  296. String FindReplaceBar::get_search_text() const {
  297. return search_text->get_text();
  298. }
  299. String FindReplaceBar::get_replace_text() const {
  300. return replace_text->get_text();
  301. }
  302. bool FindReplaceBar::is_case_sensitive() const {
  303. return case_sensitive->is_pressed();
  304. }
  305. bool FindReplaceBar::is_whole_words() const {
  306. return whole_words->is_pressed();
  307. }
  308. bool FindReplaceBar::is_selection_only() const {
  309. return selection_only->is_pressed();
  310. }
  311. void FindReplaceBar::set_error(const String &p_label) {
  312. error_label->set_text(p_label);
  313. }
  314. void FindReplaceBar::set_text_edit(TextEdit *p_text_edit) {
  315. text_edit = p_text_edit;
  316. text_edit->connect("text_changed",this,"_editor_text_changed");
  317. }
  318. void FindReplaceBar::_bind_methods() {
  319. ObjectTypeDB::bind_method("_unhandled_input",&FindReplaceBar::_unhandled_input);
  320. ObjectTypeDB::bind_method("_editor_text_changed",&FindReplaceBar::_editor_text_changed);
  321. ObjectTypeDB::bind_method("_search_text_changed",&FindReplaceBar::_search_text_changed);
  322. ObjectTypeDB::bind_method("_search_text_entered",&FindReplaceBar::_search_text_entered);
  323. ObjectTypeDB::bind_method("_search_current",&FindReplaceBar::search_current);
  324. ObjectTypeDB::bind_method("_search_next",&FindReplaceBar::search_next);
  325. ObjectTypeDB::bind_method("_search_prev",&FindReplaceBar::search_prev);
  326. ObjectTypeDB::bind_method("_replace_pressed",&FindReplaceBar::_replace);
  327. ObjectTypeDB::bind_method("_replace_all_pressed",&FindReplaceBar::_replace_all);
  328. ObjectTypeDB::bind_method("_search_options_changed",&FindReplaceBar::_search_options_changed);
  329. ObjectTypeDB::bind_method("_hide_pressed",&FindReplaceBar::_hide_bar);
  330. ADD_SIGNAL(MethodInfo("search"));
  331. }
  332. FindReplaceBar::FindReplaceBar() {
  333. replace_all_mode=false;
  334. preserve_cursor=false;
  335. text_vbc = memnew(VBoxContainer);
  336. add_child(text_vbc);
  337. HBoxContainer *search_hbc = memnew(HBoxContainer);
  338. text_vbc->add_child(search_hbc);
  339. search_text = memnew(LineEdit);
  340. search_hbc->add_child(search_text);
  341. search_text->set_custom_minimum_size(Size2(200, 0));
  342. search_text->connect("text_changed",this,"_search_text_changed");
  343. search_text->connect("text_entered",this,"_search_text_entered");
  344. find_prev = memnew(ToolButton);
  345. search_hbc->add_child(find_prev);
  346. find_prev->set_focus_mode(FOCUS_NONE);
  347. find_prev->connect("pressed",this,"_search_prev");
  348. find_next = memnew(ToolButton);
  349. search_hbc->add_child(find_next);
  350. find_next->set_focus_mode(FOCUS_NONE);
  351. find_next->connect("pressed",this,"_search_next");
  352. replace_hbc = memnew(HBoxContainer);
  353. text_vbc->add_child(replace_hbc);
  354. replace_hbc->hide();
  355. replace_text = memnew(LineEdit);
  356. replace_hbc->add_child(replace_text);
  357. replace_text->set_custom_minimum_size(Size2(200, 0));
  358. replace_text->connect("text_entered",this,"_search_text_entered");
  359. replace = memnew(ToolButton);
  360. replace_hbc->add_child(replace);
  361. replace->set_text(TTR("Replace"));
  362. replace->set_focus_mode(FOCUS_NONE);
  363. replace->connect("pressed",this,"_replace_pressed");
  364. replace_all = memnew(ToolButton);
  365. replace_hbc->add_child(replace_all);
  366. replace_all->set_text(TTR("Replace All"));
  367. replace_all->set_focus_mode(FOCUS_NONE);
  368. replace_all->connect("pressed",this,"_replace_all_pressed");
  369. Control *spacer_split = memnew( Control );
  370. spacer_split->set_custom_minimum_size(Size2(0, 1));
  371. text_vbc->add_child(spacer_split);
  372. VBoxContainer *options_vbc = memnew(VBoxContainer);
  373. add_child(options_vbc);
  374. options_vbc->set_h_size_flags(SIZE_EXPAND_FILL);
  375. HBoxContainer *search_options = memnew(HBoxContainer);
  376. options_vbc->add_child(search_options);
  377. case_sensitive = memnew(CheckBox);
  378. search_options->add_child(case_sensitive);
  379. case_sensitive->set_text(TTR("Match Case"));
  380. case_sensitive->set_focus_mode(FOCUS_NONE);
  381. case_sensitive->connect("toggled",this,"_search_options_changed");
  382. whole_words = memnew(CheckBox);
  383. search_options->add_child(whole_words);
  384. whole_words->set_text(TTR("Whole Words"));
  385. whole_words->set_focus_mode(FOCUS_NONE);
  386. whole_words->connect("toggled",this,"_search_options_changed");
  387. error_label = memnew(Label);
  388. search_options->add_child(error_label);
  389. error_label->add_color_override("font_color", Color(1,1,0,1));
  390. error_label->add_color_override("font_color_shadow", Color(0,0,0,1));
  391. error_label->add_constant_override("shadow_as_outline", 1);
  392. search_options->add_spacer();
  393. hide_button = memnew(TextureButton);
  394. search_options->add_child(hide_button);
  395. hide_button->set_focus_mode(FOCUS_NONE);
  396. hide_button->connect("pressed",this,"_hide_pressed");
  397. replace_options_hbc = memnew(HBoxContainer);
  398. options_vbc->add_child(replace_options_hbc);
  399. replace_options_hbc->hide();
  400. selection_only = memnew(CheckBox);
  401. replace_options_hbc->add_child(selection_only);
  402. selection_only->set_text(TTR("Selection Only"));
  403. selection_only->set_focus_mode(FOCUS_NONE);
  404. selection_only->connect("toggled",this,"_search_options_changed");
  405. }
  406. void FindReplaceDialog::popup_search() {
  407. set_title(TTR("Search"));
  408. replace_mc->hide();
  409. replace_label->hide();
  410. replace_vb->hide();
  411. skip->hide();
  412. popup_centered(Point2(300,190));
  413. get_ok()->set_text(TTR("Find"));
  414. search_text->grab_focus();
  415. if (text_edit->is_selection_active() && ( text_edit->get_selection_from_line() == text_edit->get_selection_to_line())) {
  416. search_text->set_text( text_edit->get_selection_text() );
  417. }
  418. search_text->select_all();
  419. error_label->set_text("");
  420. }
  421. void FindReplaceDialog::popup_replace() {
  422. set_title(TTR("Replace"));
  423. bool do_selection=(text_edit->is_selection_active() && text_edit->get_selection_from_line() < text_edit->get_selection_to_line());
  424. set_replace_selection_only(do_selection);
  425. if (!do_selection && text_edit->is_selection_active()) {
  426. search_text->set_text(text_edit->get_selection_text());
  427. }
  428. replace_mc->show();
  429. replace_label->show();
  430. replace_vb->show();
  431. popup_centered(Point2(300,300));
  432. if (search_text->get_text()!="" && replace_text->get_text()=="") {
  433. search_text->select(0,0);
  434. replace_text->grab_focus();
  435. } else {
  436. search_text->grab_focus();
  437. search_text->select_all();
  438. }
  439. error_label->set_text("");
  440. if (prompt->is_pressed()) {
  441. skip->show();
  442. get_ok()->set_text(TTR("Next"));
  443. selection_only->set_disabled(true);
  444. } else {
  445. skip->hide();
  446. get_ok()->set_text(TTR("Replace"));
  447. selection_only->set_disabled(false);
  448. }
  449. }
  450. void FindReplaceDialog::_search_callback() {
  451. if (is_replace_mode())
  452. _replace();
  453. else
  454. _search();
  455. }
  456. void FindReplaceDialog::_replace_skip_callback() {
  457. _search();
  458. }
  459. void FindReplaceDialog::_replace() {
  460. text_edit->begin_complex_operation();
  461. if (is_replace_all_mode()) {
  462. //line as x so it gets priority in comparison, column as y
  463. Point2i orig_cursor(text_edit->cursor_get_line(),text_edit->cursor_get_column());
  464. Point2i prev_match=Point2(-1,-1);
  465. bool selection_enabled = text_edit->is_selection_active();
  466. Point2i selection_begin,selection_end;
  467. if (selection_enabled) {
  468. selection_begin=Point2i(text_edit->get_selection_from_line(),text_edit->get_selection_from_column());
  469. selection_end=Point2i(text_edit->get_selection_to_line(),text_edit->get_selection_to_column());
  470. }
  471. int vsval = text_edit->get_v_scroll();
  472. //int hsval = text_edit->get_h_scroll();
  473. text_edit->cursor_set_line(0);
  474. text_edit->cursor_set_column(0);
  475. int rc=0;
  476. while(_search()) {
  477. if (!text_edit->is_selection_active()) {
  478. //search selects
  479. break;
  480. }
  481. //replace area
  482. Point2i match_from(text_edit->get_selection_from_line(),text_edit->get_selection_from_column());
  483. Point2i match_to(text_edit->get_selection_to_line(),text_edit->get_selection_to_column());
  484. if (match_from < prev_match)
  485. break; //done
  486. prev_match=match_to;
  487. if (selection_enabled && is_replace_selection_only()) {
  488. if (match_from<selection_begin || match_to>selection_end)
  489. continue;
  490. //replace but adjust selection bounds
  491. text_edit->insert_text_at_cursor(get_replace_text());
  492. if (match_to.x==selection_end.x)
  493. selection_end.y+=get_replace_text().length() - get_search_text().length();
  494. } else {
  495. //just replace
  496. text_edit->insert_text_at_cursor(get_replace_text());
  497. }
  498. rc++;
  499. }
  500. //restore editor state (selection, cursor, scroll)
  501. text_edit->cursor_set_line(orig_cursor.x);
  502. text_edit->cursor_set_column(orig_cursor.y);
  503. if (selection_enabled && is_replace_selection_only()) {
  504. //reselect
  505. text_edit->select(selection_begin.x,selection_begin.y,selection_end.x,selection_end.y);
  506. } else {
  507. text_edit->deselect();
  508. }
  509. text_edit->set_v_scroll(vsval);
  510. // text_edit->set_h_scroll(hsval);
  511. error_label->set_text(vformat(TTR("Replaced %d ocurrence(s)."),rc));
  512. //hide();
  513. } else {
  514. if (text_edit->get_selection_text()==get_search_text()) {
  515. text_edit->insert_text_at_cursor(get_replace_text());
  516. }
  517. _search();
  518. }
  519. text_edit->end_complex_operation();
  520. }
  521. bool FindReplaceDialog::_search() {
  522. String text=get_search_text();
  523. uint32_t flags=0;
  524. if (is_whole_words())
  525. flags|=TextEdit::SEARCH_WHOLE_WORDS;
  526. if (is_case_sensitive())
  527. flags|=TextEdit::SEARCH_MATCH_CASE;
  528. if (is_backwards())
  529. flags|=TextEdit::SEARCH_BACKWARDS;
  530. int line=text_edit->cursor_get_line(),col=text_edit->cursor_get_column();
  531. if (is_backwards()) {
  532. col-=1;
  533. if (col<0) {
  534. line-=1;
  535. if (line<0) {
  536. line=text_edit->get_line_count()-1;
  537. }
  538. col=text_edit->get_line(line).length();
  539. }
  540. }
  541. bool found = text_edit->search(text,flags,line,col,line,col);
  542. if (found) {
  543. // print_line("found");
  544. text_edit->cursor_set_line(line);
  545. if (is_backwards())
  546. text_edit->cursor_set_column(col);
  547. else
  548. text_edit->cursor_set_column(col+text.length());
  549. text_edit->select(line,col,line,col+text.length());
  550. set_error("");
  551. return true;
  552. } else {
  553. set_error(TTR("Not found!"));
  554. return false;
  555. }
  556. }
  557. void FindReplaceDialog::_prompt_changed() {
  558. if (prompt->is_pressed()) {
  559. skip->show();
  560. get_ok()->set_text(TTR("Next"));
  561. selection_only->set_disabled(true);
  562. } else {
  563. skip->hide();
  564. get_ok()->set_text(TTR("Replace"));
  565. selection_only->set_disabled(false);
  566. }
  567. }
  568. void FindReplaceDialog::_skip_pressed() {
  569. _replace_skip_callback();
  570. }
  571. bool FindReplaceDialog::is_replace_mode() const {
  572. return replace_text->is_visible();
  573. }
  574. bool FindReplaceDialog::is_replace_all_mode() const {
  575. return !prompt->is_pressed();
  576. }
  577. bool FindReplaceDialog::is_replace_selection_only() const {
  578. return selection_only->is_pressed();
  579. }
  580. void FindReplaceDialog::set_replace_selection_only(bool p_enable){
  581. selection_only->set_pressed(p_enable);
  582. }
  583. void FindReplaceDialog::ok_pressed() {
  584. _search_callback();
  585. }
  586. void FindReplaceDialog::_search_text_entered(const String& p_text) {
  587. if (replace_text->is_visible())
  588. return;
  589. emit_signal("search");
  590. _search();
  591. }
  592. void FindReplaceDialog::_replace_text_entered(const String& p_text) {
  593. if (!replace_text->is_visible())
  594. return;
  595. emit_signal("search");
  596. _replace();
  597. }
  598. String FindReplaceDialog::get_search_text() const {
  599. return search_text->get_text();
  600. }
  601. String FindReplaceDialog::get_replace_text() const {
  602. return replace_text->get_text();
  603. }
  604. bool FindReplaceDialog::is_whole_words() const {
  605. return whole_words->is_pressed();
  606. }
  607. bool FindReplaceDialog::is_case_sensitive() const {
  608. return case_sensitive->is_pressed();
  609. }
  610. bool FindReplaceDialog::is_backwards() const {
  611. return backwards->is_pressed();
  612. }
  613. void FindReplaceDialog::set_error(const String& p_error) {
  614. error_label->set_text(p_error);
  615. }
  616. void FindReplaceDialog::set_text_edit(TextEdit *p_text_edit) {
  617. text_edit=p_text_edit;
  618. }
  619. void FindReplaceDialog::search_next() {
  620. _search();
  621. }
  622. void FindReplaceDialog::_bind_methods() {
  623. ObjectTypeDB::bind_method("_search_text_entered",&FindReplaceDialog::_search_text_entered);
  624. ObjectTypeDB::bind_method("_replace_text_entered",&FindReplaceDialog::_replace_text_entered);
  625. ObjectTypeDB::bind_method("_prompt_changed",&FindReplaceDialog::_prompt_changed);
  626. ObjectTypeDB::bind_method("_skip_pressed",&FindReplaceDialog::_skip_pressed);
  627. ADD_SIGNAL(MethodInfo("search"));
  628. ADD_SIGNAL(MethodInfo("skip"));
  629. }
  630. FindReplaceDialog::FindReplaceDialog() {
  631. set_self_opacity(0.8);
  632. VBoxContainer *vb = memnew( VBoxContainer );
  633. add_child(vb);
  634. set_child_rect(vb);
  635. search_text = memnew( LineEdit );
  636. vb->add_margin_child(TTR("Search"),search_text);
  637. search_text->connect("text_entered", this,"_search_text_entered");
  638. //search_text->set_self_opacity(0.7);
  639. replace_label = memnew( Label);
  640. replace_label->set_text(TTR("Replace By"));
  641. vb->add_child(replace_label);
  642. replace_mc= memnew( MarginContainer);
  643. vb->add_child(replace_mc);
  644. replace_text = memnew( LineEdit );
  645. replace_text->set_anchor( MARGIN_RIGHT, ANCHOR_END );
  646. replace_text->set_begin( Point2(15,132) );
  647. replace_text->set_end( Point2(15,135) );
  648. //replace_text->set_self_opacity(0.7);
  649. replace_mc->add_child(replace_text);
  650. replace_text->connect("text_entered", this,"_replace_text_entered");
  651. MarginContainer *opt_mg = memnew( MarginContainer );
  652. vb->add_child(opt_mg);
  653. VBoxContainer *svb = memnew( VBoxContainer);
  654. opt_mg->add_child(svb);
  655. svb ->add_child(memnew(Label));
  656. whole_words = memnew( CheckButton );
  657. whole_words->set_text(TTR("Whole Words"));
  658. svb->add_child(whole_words);
  659. case_sensitive = memnew( CheckButton );
  660. case_sensitive->set_text(TTR("Case Sensitive"));
  661. svb->add_child(case_sensitive);
  662. backwards = memnew( CheckButton );
  663. backwards->set_text(TTR("Backwards"));
  664. svb->add_child(backwards);
  665. opt_mg = memnew( MarginContainer );
  666. vb->add_child(opt_mg);
  667. VBoxContainer *rvb = memnew( VBoxContainer);
  668. opt_mg->add_child(rvb);
  669. replace_vb=rvb;
  670. // rvb ->add_child(memnew(HSeparator));
  671. rvb ->add_child(memnew(Label));
  672. prompt = memnew( CheckButton );
  673. prompt->set_text(TTR("Prompt On Replace"));
  674. rvb->add_child(prompt);
  675. prompt->connect("pressed", this,"_prompt_changed");
  676. selection_only = memnew( CheckButton );
  677. selection_only->set_text(TTR("Selection Only"));
  678. rvb->add_child(selection_only);
  679. int margin = get_constant("margin","Dialogs");
  680. int button_margin = get_constant("button_margin","Dialogs");
  681. skip = memnew( Button );
  682. skip->set_anchor( MARGIN_LEFT, ANCHOR_END );
  683. skip->set_anchor( MARGIN_TOP, ANCHOR_END );
  684. skip->set_anchor( MARGIN_RIGHT, ANCHOR_END );
  685. skip->set_anchor( MARGIN_BOTTOM, ANCHOR_END );
  686. skip->set_begin( Point2( 70, button_margin ) );
  687. skip->set_end( Point2( 10, margin ) );
  688. skip->set_text(TTR("Skip"));
  689. add_child(skip);
  690. skip->connect("pressed", this,"_skip_pressed");
  691. error_label = memnew( Label );
  692. error_label->set_align(Label::ALIGN_CENTER);
  693. error_label->add_color_override("font_color",Color(1,0.4,0.3));
  694. error_label->add_color_override("font_color_shadow",Color(0,0,0,0.2));
  695. error_label->add_constant_override("shadow_as_outline",1);
  696. vb->add_child(error_label);
  697. set_hide_on_ok(false);
  698. }
  699. /*** CODE EDITOR ****/
  700. void CodeTextEditor::_line_col_changed() {
  701. String text = String()+TTR("Line:")+" "+itos(text_editor->cursor_get_line()+1)+", "+TTR("Col:")+" "+itos(text_editor->cursor_get_column());
  702. line_col->set_text(text);
  703. }
  704. void CodeTextEditor::_text_changed() {
  705. code_complete_timer->start();
  706. idle->start();
  707. }
  708. void CodeTextEditor::_code_complete_timer_timeout() {
  709. if (!is_visible())
  710. return;
  711. if (enable_complete_timer)
  712. text_editor->query_code_comple();
  713. }
  714. void CodeTextEditor::_complete_request() {
  715. List<String> entries;
  716. _code_complete_script(text_editor->get_text_for_completion(),&entries);
  717. // print_line("COMPLETE: "+p_request);
  718. if (entries.size()==0)
  719. return;
  720. Vector<String> strs;
  721. strs.resize(entries.size());
  722. int i=0;
  723. for(List<String>::Element *E=entries.front();E;E=E->next()) {
  724. strs[i++]=E->get();
  725. }
  726. text_editor->code_complete(strs);
  727. }
  728. void CodeTextEditor::set_error(const String& p_error) {
  729. if (p_error!="") {
  730. error->set_text(p_error);
  731. error->show();
  732. } else {
  733. error->hide();
  734. }
  735. }
  736. void CodeTextEditor::_update_font() {
  737. // FONTS
  738. String editor_font = EDITOR_DEF("text_editor/font", "");
  739. bool font_overrode = false;
  740. if (editor_font!="") {
  741. Ref<Font> fnt = ResourceLoader::load(editor_font);
  742. if (fnt.is_valid()) {
  743. text_editor->add_font_override("font",fnt);
  744. font_overrode = true;
  745. }
  746. }
  747. if(!font_overrode)
  748. text_editor->add_font_override("font",get_font("source","EditorFonts"));
  749. }
  750. void CodeTextEditor::_on_settings_change() {
  751. _update_font();
  752. // AUTO BRACE COMPLETION
  753. text_editor->set_auto_brace_completion(
  754. EDITOR_DEF("text_editor/auto_brace_complete", true)
  755. );
  756. code_complete_timer->set_wait_time(
  757. EDITOR_DEF("text_editor/code_complete_delay",.3f)
  758. );
  759. enable_complete_timer = EDITOR_DEF("text_editor/enable_code_completion_delay",true);
  760. // call hint settings
  761. text_editor->set_callhint_settings(
  762. EDITOR_DEF("text_editor/put_callhint_tooltip_below_current_line", true),
  763. EDITOR_DEF("text_editor/callhint_tooltip_offset", Vector2())
  764. );
  765. }
  766. void CodeTextEditor::_text_changed_idle_timeout() {
  767. _validate_script();
  768. }
  769. void CodeTextEditor::_notification(int p_what) {
  770. if (p_what==EditorSettings::NOTIFICATION_EDITOR_SETTINGS_CHANGED)
  771. _load_theme_settings();
  772. if (p_what==NOTIFICATION_ENTER_TREE) {
  773. _update_font();
  774. }
  775. }
  776. void CodeTextEditor::_bind_methods() {
  777. ObjectTypeDB::bind_method("_line_col_changed",&CodeTextEditor::_line_col_changed);
  778. ObjectTypeDB::bind_method("_text_changed",&CodeTextEditor::_text_changed);
  779. ObjectTypeDB::bind_method("_on_settings_change",&CodeTextEditor::_on_settings_change);
  780. ObjectTypeDB::bind_method("_text_changed_idle_timeout",&CodeTextEditor::_text_changed_idle_timeout);
  781. ObjectTypeDB::bind_method("_code_complete_timer_timeout",&CodeTextEditor::_code_complete_timer_timeout);
  782. ObjectTypeDB::bind_method("_complete_request",&CodeTextEditor::_complete_request);
  783. }
  784. CodeTextEditor::CodeTextEditor() {
  785. find_replace_bar = memnew( FindReplaceBar );
  786. add_child(find_replace_bar);
  787. find_replace_bar->set_h_size_flags(SIZE_EXPAND_FILL);
  788. find_replace_bar->hide();
  789. text_editor = memnew( TextEdit );
  790. add_child(text_editor);
  791. text_editor->set_v_size_flags(SIZE_EXPAND_FILL);
  792. find_replace_bar->set_text_edit(text_editor);
  793. text_editor->set_show_line_numbers(true);
  794. text_editor->set_brace_matching(true);
  795. text_editor->set_auto_indent(true);
  796. MarginContainer *status_mc = memnew( MarginContainer );
  797. add_child(status_mc);
  798. status_mc->set("custom_constants/margin_left", 2);
  799. status_mc->set("custom_constants/margin_top", 5);
  800. status_mc->set("custom_constants/margin_right", 2);
  801. status_mc->set("custom_constants/margin_bottom", 1);
  802. HBoxContainer *status_bar = memnew( HBoxContainer );
  803. status_mc->add_child(status_bar);
  804. status_bar->set_h_size_flags(SIZE_EXPAND_FILL);
  805. idle = memnew( Timer );
  806. add_child(idle);
  807. idle->set_one_shot(true);
  808. idle->set_wait_time(EDITOR_DEF("text_editor/idle_parse_delay",2));
  809. code_complete_timer = memnew(Timer);
  810. add_child(code_complete_timer);
  811. code_complete_timer->set_one_shot(true);
  812. enable_complete_timer = EDITOR_DEF("text_editor/enable_code_completion_delay",true);
  813. code_complete_timer->set_wait_time(EDITOR_DEF("text_editor/code_complete_delay",.3f));
  814. error = memnew( Label );
  815. status_bar->add_child(error);
  816. error->hide();
  817. error->set_valign(Label::VALIGN_CENTER);
  818. error->add_color_override("font_color",Color(1,0.7,0.6,0.9));
  819. status_bar->add_spacer();
  820. line_col = memnew( Label );
  821. status_bar->add_child(line_col);
  822. line_col->set_valign(Label::VALIGN_CENTER);
  823. text_editor->connect("cursor_changed", this,"_line_col_changed");
  824. text_editor->connect("text_changed", this,"_text_changed");
  825. text_editor->connect("request_completion", this,"_complete_request");
  826. Vector<String> cs;
  827. cs.push_back(".");
  828. cs.push_back(",");
  829. cs.push_back("(");
  830. text_editor->set_completion(true,cs);
  831. idle->connect("timeout", this,"_text_changed_idle_timeout");
  832. code_complete_timer->connect("timeout", this,"_code_complete_timer_timeout");
  833. EditorSettings::get_singleton()->connect("settings_changed",this,"_on_settings_change");
  834. }