code_editor.cpp 36 KB

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