code_editor.cpp 55 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755
  1. /*************************************************************************/
  2. /* code_editor.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "code_editor.h"
  31. #include "core/input/input.h"
  32. #include "core/os/keyboard.h"
  33. #include "core/string_builder.h"
  34. #include "editor/editor_scale.h"
  35. #include "editor_node.h"
  36. #include "editor_settings.h"
  37. #include "scene/gui/margin_container.h"
  38. #include "scene/gui/separator.h"
  39. #include "scene/resources/dynamic_font.h"
  40. void GotoLineDialog::popup_find_line(TextEdit *p_edit) {
  41. text_editor = p_edit;
  42. line->set_text(itos(text_editor->cursor_get_line()));
  43. line->select_all();
  44. popup_centered(Size2(180, 80) * EDSCALE);
  45. line->grab_focus();
  46. }
  47. int GotoLineDialog::get_line() const {
  48. return line->get_text().to_int();
  49. }
  50. void GotoLineDialog::ok_pressed() {
  51. if (get_line() < 1 || get_line() > text_editor->get_line_count()) {
  52. return;
  53. }
  54. text_editor->unfold_line(get_line() - 1);
  55. text_editor->cursor_set_line(get_line() - 1);
  56. hide();
  57. }
  58. GotoLineDialog::GotoLineDialog() {
  59. set_title(TTR("Go to Line"));
  60. VBoxContainer *vbc = memnew(VBoxContainer);
  61. vbc->set_anchor_and_margin(MARGIN_LEFT, Control::ANCHOR_BEGIN, 8 * EDSCALE);
  62. vbc->set_anchor_and_margin(MARGIN_TOP, Control::ANCHOR_BEGIN, 8 * EDSCALE);
  63. vbc->set_anchor_and_margin(MARGIN_RIGHT, Control::ANCHOR_END, -8 * EDSCALE);
  64. vbc->set_anchor_and_margin(MARGIN_BOTTOM, Control::ANCHOR_END, -8 * EDSCALE);
  65. add_child(vbc);
  66. Label *l = memnew(Label);
  67. l->set_text(TTR("Line Number:"));
  68. vbc->add_child(l);
  69. line = memnew(LineEdit);
  70. vbc->add_child(line);
  71. register_text_enter(line);
  72. text_editor = nullptr;
  73. set_hide_on_ok(false);
  74. }
  75. void FindReplaceBar::_notification(int p_what) {
  76. if (p_what == NOTIFICATION_READY) {
  77. find_prev->set_icon(get_theme_icon("MoveUp", "EditorIcons"));
  78. find_next->set_icon(get_theme_icon("MoveDown", "EditorIcons"));
  79. hide_button->set_normal_texture(get_theme_icon("Close", "EditorIcons"));
  80. hide_button->set_hover_texture(get_theme_icon("Close", "EditorIcons"));
  81. hide_button->set_pressed_texture(get_theme_icon("Close", "EditorIcons"));
  82. hide_button->set_custom_minimum_size(hide_button->get_normal_texture()->get_size());
  83. } else if (p_what == NOTIFICATION_VISIBILITY_CHANGED) {
  84. set_process_unhandled_input(is_visible_in_tree());
  85. } else if (p_what == EditorSettings::NOTIFICATION_EDITOR_SETTINGS_CHANGED) {
  86. find_prev->set_icon(get_theme_icon("MoveUp", "EditorIcons"));
  87. find_next->set_icon(get_theme_icon("MoveDown", "EditorIcons"));
  88. hide_button->set_normal_texture(get_theme_icon("Close", "EditorIcons"));
  89. hide_button->set_hover_texture(get_theme_icon("Close", "EditorIcons"));
  90. hide_button->set_pressed_texture(get_theme_icon("Close", "EditorIcons"));
  91. hide_button->set_custom_minimum_size(hide_button->get_normal_texture()->get_size());
  92. } else if (p_what == NOTIFICATION_THEME_CHANGED) {
  93. matches_label->add_theme_color_override("font_color", results_count > 0 ? get_theme_color("font_color", "Label") : get_theme_color("error_color", "Editor"));
  94. }
  95. }
  96. void FindReplaceBar::_unhandled_input(const Ref<InputEvent> &p_event) {
  97. Ref<InputEventKey> k = p_event;
  98. if (k.is_valid()) {
  99. if (k->is_pressed() && (text_edit->has_focus() || vbc_lineedit->is_a_parent_of(get_focus_owner()))) {
  100. bool accepted = true;
  101. switch (k->get_keycode()) {
  102. case KEY_ESCAPE: {
  103. _hide_bar();
  104. } break;
  105. default: {
  106. accepted = false;
  107. } break;
  108. }
  109. if (accepted) {
  110. accept_event();
  111. }
  112. }
  113. }
  114. }
  115. bool FindReplaceBar::_search(uint32_t p_flags, int p_from_line, int p_from_col) {
  116. int line, col;
  117. String text = get_search_text();
  118. bool found = text_edit->search(text, p_flags, p_from_line, p_from_col, line, col);
  119. if (found) {
  120. if (!preserve_cursor) {
  121. text_edit->unfold_line(line);
  122. text_edit->cursor_set_line(line, false);
  123. text_edit->cursor_set_column(col + text.length(), false);
  124. text_edit->center_viewport_to_cursor();
  125. text_edit->select(line, col, line, col + text.length());
  126. }
  127. text_edit->set_search_text(text);
  128. text_edit->set_search_flags(p_flags);
  129. text_edit->set_current_search_result(line, col);
  130. result_line = line;
  131. result_col = col;
  132. _update_results_count();
  133. } else {
  134. results_count = 0;
  135. result_line = -1;
  136. result_col = -1;
  137. text_edit->set_search_text("");
  138. text_edit->set_search_flags(p_flags);
  139. text_edit->set_current_search_result(line, col);
  140. }
  141. _update_matches_label();
  142. return found;
  143. }
  144. void FindReplaceBar::_replace() {
  145. if (result_line != -1 && result_col != -1) {
  146. text_edit->begin_complex_operation();
  147. text_edit->unfold_line(result_line);
  148. text_edit->select(result_line, result_col, result_line, result_col + get_search_text().length());
  149. text_edit->insert_text_at_cursor(get_replace_text());
  150. text_edit->end_complex_operation();
  151. results_count = -1;
  152. }
  153. if (!search_current()) {
  154. search_next();
  155. }
  156. }
  157. void FindReplaceBar::_replace_all() {
  158. text_edit->disconnect("text_changed", callable_mp(this, &FindReplaceBar::_editor_text_changed));
  159. // Line as x so it gets priority in comparison, column as y.
  160. Point2i orig_cursor(text_edit->cursor_get_line(), text_edit->cursor_get_column());
  161. Point2i prev_match = Point2(-1, -1);
  162. bool selection_enabled = text_edit->is_selection_active();
  163. Point2i selection_begin, selection_end;
  164. if (selection_enabled) {
  165. selection_begin = Point2i(text_edit->get_selection_from_line(), text_edit->get_selection_from_column());
  166. selection_end = Point2i(text_edit->get_selection_to_line(), text_edit->get_selection_to_column());
  167. }
  168. int vsval = text_edit->get_v_scroll();
  169. text_edit->cursor_set_line(0);
  170. text_edit->cursor_set_column(0);
  171. String replace_text = get_replace_text();
  172. int search_text_len = get_search_text().length();
  173. int rc = 0;
  174. replace_all_mode = true;
  175. text_edit->begin_complex_operation();
  176. if (selection_enabled && is_selection_only()) {
  177. text_edit->cursor_set_line(selection_begin.width);
  178. text_edit->cursor_set_column(selection_begin.height);
  179. }
  180. if (search_current()) {
  181. do {
  182. // replace area
  183. Point2i match_from(result_line, result_col);
  184. Point2i match_to(result_line, result_col + search_text_len);
  185. if (match_from < prev_match) {
  186. break; // Done.
  187. }
  188. prev_match = Point2i(result_line, result_col + replace_text.length());
  189. text_edit->unfold_line(result_line);
  190. text_edit->select(result_line, result_col, result_line, match_to.y);
  191. if (selection_enabled && is_selection_only()) {
  192. if (match_from < selection_begin || match_to > selection_end) {
  193. break; // Done.
  194. }
  195. // Replace but adjust selection bounds.
  196. text_edit->insert_text_at_cursor(replace_text);
  197. if (match_to.x == selection_end.x) {
  198. selection_end.y += replace_text.length() - search_text_len;
  199. }
  200. } else {
  201. // Just replace.
  202. text_edit->insert_text_at_cursor(replace_text);
  203. }
  204. rc++;
  205. } while (search_next());
  206. }
  207. text_edit->end_complex_operation();
  208. replace_all_mode = false;
  209. // Restore editor state (selection, cursor, scroll).
  210. text_edit->cursor_set_line(orig_cursor.x);
  211. text_edit->cursor_set_column(orig_cursor.y);
  212. if (selection_enabled && is_selection_only()) {
  213. // Reselect.
  214. text_edit->select(selection_begin.x, selection_begin.y, selection_end.x, selection_end.y);
  215. } else {
  216. text_edit->deselect();
  217. }
  218. text_edit->set_v_scroll(vsval);
  219. matches_label->add_theme_color_override("font_color", rc > 0 ? get_theme_color("font_color", "Label") : get_theme_color("error_color", "Editor"));
  220. matches_label->set_text(vformat(TTR("%d replaced."), rc));
  221. text_edit->call_deferred("connect", "text_changed", Callable(this, "_editor_text_changed"));
  222. results_count = -1;
  223. }
  224. void FindReplaceBar::_get_search_from(int &r_line, int &r_col) {
  225. r_line = text_edit->cursor_get_line();
  226. r_col = text_edit->cursor_get_column();
  227. if (text_edit->is_selection_active() && is_selection_only()) {
  228. return;
  229. }
  230. if (r_line == result_line && r_col >= result_col && r_col <= result_col + get_search_text().length()) {
  231. r_col = result_col;
  232. }
  233. }
  234. void FindReplaceBar::_update_results_count() {
  235. if (results_count != -1) {
  236. return;
  237. }
  238. results_count = 0;
  239. String searched = get_search_text();
  240. if (searched.empty()) {
  241. return;
  242. }
  243. String full_text = text_edit->get_text();
  244. int from_pos = 0;
  245. while (true) {
  246. int pos = is_case_sensitive() ? full_text.find(searched, from_pos) : full_text.findn(searched, from_pos);
  247. if (pos == -1) {
  248. break;
  249. }
  250. int pos_subsequent = pos + searched.length();
  251. if (is_whole_words()) {
  252. from_pos = pos + 1; // Making sure we won't hit the same match next time, if we get out via a continue.
  253. if (pos > 0 && !(is_symbol(full_text[pos - 1]) || full_text[pos - 1] == '\n')) {
  254. continue;
  255. }
  256. if (pos_subsequent < full_text.length() && !(is_symbol(full_text[pos_subsequent]) || full_text[pos_subsequent] == '\n')) {
  257. continue;
  258. }
  259. }
  260. results_count++;
  261. from_pos = pos_subsequent;
  262. }
  263. }
  264. void FindReplaceBar::_update_matches_label() {
  265. if (search_text->get_text().empty() || results_count == -1) {
  266. matches_label->hide();
  267. } else {
  268. matches_label->show();
  269. matches_label->add_theme_color_override("font_color", results_count > 0 ? get_theme_color("font_color", "Label") : get_theme_color("error_color", "Editor"));
  270. matches_label->set_text(vformat(results_count == 1 ? TTR("%d match.") : TTR("%d matches."), results_count));
  271. }
  272. }
  273. bool FindReplaceBar::search_current() {
  274. uint32_t flags = 0;
  275. if (is_whole_words()) {
  276. flags |= TextEdit::SEARCH_WHOLE_WORDS;
  277. }
  278. if (is_case_sensitive()) {
  279. flags |= TextEdit::SEARCH_MATCH_CASE;
  280. }
  281. int line, col;
  282. _get_search_from(line, col);
  283. return _search(flags, line, col);
  284. }
  285. bool FindReplaceBar::search_prev() {
  286. if (!is_visible()) {
  287. popup_search(true);
  288. }
  289. uint32_t flags = 0;
  290. String text = get_search_text();
  291. if (is_whole_words()) {
  292. flags |= TextEdit::SEARCH_WHOLE_WORDS;
  293. }
  294. if (is_case_sensitive()) {
  295. flags |= TextEdit::SEARCH_MATCH_CASE;
  296. }
  297. flags |= TextEdit::SEARCH_BACKWARDS;
  298. int line, col;
  299. _get_search_from(line, col);
  300. if (text_edit->is_selection_active()) {
  301. col--; // Skip currently selected word.
  302. }
  303. col -= text.length();
  304. if (col < 0) {
  305. line -= 1;
  306. if (line < 0) {
  307. line = text_edit->get_line_count() - 1;
  308. }
  309. col = text_edit->get_line(line).length();
  310. }
  311. return _search(flags, line, col);
  312. }
  313. bool FindReplaceBar::search_next() {
  314. if (!is_visible()) {
  315. popup_search(true);
  316. }
  317. uint32_t flags = 0;
  318. String text;
  319. if (replace_all_mode) {
  320. text = get_replace_text();
  321. } else {
  322. text = get_search_text();
  323. }
  324. if (is_whole_words()) {
  325. flags |= TextEdit::SEARCH_WHOLE_WORDS;
  326. }
  327. if (is_case_sensitive()) {
  328. flags |= TextEdit::SEARCH_MATCH_CASE;
  329. }
  330. int line, col;
  331. _get_search_from(line, col);
  332. if (line == result_line && col == result_col) {
  333. col += text.length();
  334. if (col > text_edit->get_line(line).length()) {
  335. line += 1;
  336. if (line >= text_edit->get_line_count()) {
  337. line = 0;
  338. }
  339. col = 0;
  340. }
  341. }
  342. return _search(flags, line, col);
  343. }
  344. void FindReplaceBar::_hide_bar() {
  345. if (replace_text->has_focus() || search_text->has_focus()) {
  346. text_edit->grab_focus();
  347. }
  348. text_edit->set_search_text("");
  349. result_line = -1;
  350. result_col = -1;
  351. hide();
  352. }
  353. void FindReplaceBar::_show_search(bool p_focus_replace, bool p_show_only) {
  354. show();
  355. if (p_show_only) {
  356. return;
  357. }
  358. if (p_focus_replace) {
  359. search_text->deselect();
  360. replace_text->call_deferred("grab_focus");
  361. } else {
  362. replace_text->deselect();
  363. search_text->call_deferred("grab_focus");
  364. }
  365. if (text_edit->is_selection_active() && !selection_only->is_pressed()) {
  366. search_text->set_text(text_edit->get_selection_text());
  367. }
  368. if (!get_search_text().empty()) {
  369. if (p_focus_replace) {
  370. replace_text->select_all();
  371. replace_text->set_cursor_position(replace_text->get_text().length());
  372. } else {
  373. search_text->select_all();
  374. search_text->set_cursor_position(search_text->get_text().length());
  375. }
  376. results_count = -1;
  377. _update_results_count();
  378. _update_matches_label();
  379. }
  380. }
  381. void FindReplaceBar::popup_search(bool p_show_only) {
  382. replace_text->hide();
  383. hbc_button_replace->hide();
  384. hbc_option_replace->hide();
  385. _show_search(false, p_show_only);
  386. }
  387. void FindReplaceBar::popup_replace() {
  388. if (!replace_text->is_visible_in_tree()) {
  389. replace_text->show();
  390. hbc_button_replace->show();
  391. hbc_option_replace->show();
  392. }
  393. selection_only->set_pressed((text_edit->is_selection_active() && text_edit->get_selection_from_line() < text_edit->get_selection_to_line()));
  394. _show_search(is_visible() || text_edit->is_selection_active());
  395. }
  396. void FindReplaceBar::_search_options_changed(bool p_pressed) {
  397. results_count = -1;
  398. search_current();
  399. }
  400. void FindReplaceBar::_editor_text_changed() {
  401. results_count = -1;
  402. if (is_visible_in_tree()) {
  403. preserve_cursor = true;
  404. search_current();
  405. preserve_cursor = false;
  406. }
  407. }
  408. void FindReplaceBar::_search_text_changed(const String &p_text) {
  409. results_count = -1;
  410. search_current();
  411. }
  412. void FindReplaceBar::_search_text_entered(const String &p_text) {
  413. if (Input::get_singleton()->is_key_pressed(KEY_SHIFT)) {
  414. search_prev();
  415. } else {
  416. search_next();
  417. }
  418. }
  419. void FindReplaceBar::_replace_text_entered(const String &p_text) {
  420. if (selection_only->is_pressed() && text_edit->is_selection_active()) {
  421. _replace_all();
  422. _hide_bar();
  423. } else if (Input::get_singleton()->is_key_pressed(KEY_SHIFT)) {
  424. _replace();
  425. search_prev();
  426. } else {
  427. _replace();
  428. }
  429. }
  430. String FindReplaceBar::get_search_text() const {
  431. return search_text->get_text();
  432. }
  433. String FindReplaceBar::get_replace_text() const {
  434. return replace_text->get_text();
  435. }
  436. bool FindReplaceBar::is_case_sensitive() const {
  437. return case_sensitive->is_pressed();
  438. }
  439. bool FindReplaceBar::is_whole_words() const {
  440. return whole_words->is_pressed();
  441. }
  442. bool FindReplaceBar::is_selection_only() const {
  443. return selection_only->is_pressed();
  444. }
  445. void FindReplaceBar::set_error(const String &p_label) {
  446. emit_signal("error", p_label);
  447. }
  448. void FindReplaceBar::set_text_edit(TextEdit *p_text_edit) {
  449. results_count = -1;
  450. text_edit = p_text_edit;
  451. text_edit->connect("text_changed", callable_mp(this, &FindReplaceBar::_editor_text_changed));
  452. }
  453. void FindReplaceBar::_bind_methods() {
  454. ClassDB::bind_method("_unhandled_input", &FindReplaceBar::_unhandled_input);
  455. ClassDB::bind_method("_search_current", &FindReplaceBar::search_current);
  456. ADD_SIGNAL(MethodInfo("search"));
  457. ADD_SIGNAL(MethodInfo("error"));
  458. }
  459. FindReplaceBar::FindReplaceBar() {
  460. results_count = -1;
  461. replace_all_mode = false;
  462. preserve_cursor = false;
  463. vbc_lineedit = memnew(VBoxContainer);
  464. add_child(vbc_lineedit);
  465. vbc_lineedit->set_alignment(ALIGN_CENTER);
  466. vbc_lineedit->set_h_size_flags(SIZE_EXPAND_FILL);
  467. VBoxContainer *vbc_button = memnew(VBoxContainer);
  468. add_child(vbc_button);
  469. VBoxContainer *vbc_option = memnew(VBoxContainer);
  470. add_child(vbc_option);
  471. HBoxContainer *hbc_button_search = memnew(HBoxContainer);
  472. vbc_button->add_child(hbc_button_search);
  473. hbc_button_search->set_alignment(ALIGN_END);
  474. hbc_button_replace = memnew(HBoxContainer);
  475. vbc_button->add_child(hbc_button_replace);
  476. hbc_button_replace->set_alignment(ALIGN_END);
  477. HBoxContainer *hbc_option_search = memnew(HBoxContainer);
  478. vbc_option->add_child(hbc_option_search);
  479. hbc_option_replace = memnew(HBoxContainer);
  480. vbc_option->add_child(hbc_option_replace);
  481. // search toolbar
  482. search_text = memnew(LineEdit);
  483. vbc_lineedit->add_child(search_text);
  484. search_text->set_custom_minimum_size(Size2(100 * EDSCALE, 0));
  485. search_text->connect("text_changed", callable_mp(this, &FindReplaceBar::_search_text_changed));
  486. search_text->connect("text_entered", callable_mp(this, &FindReplaceBar::_search_text_entered));
  487. matches_label = memnew(Label);
  488. hbc_button_search->add_child(matches_label);
  489. matches_label->hide();
  490. find_prev = memnew(ToolButton);
  491. hbc_button_search->add_child(find_prev);
  492. find_prev->set_focus_mode(FOCUS_NONE);
  493. find_prev->connect("pressed", callable_mp(this, &FindReplaceBar::search_prev));
  494. find_next = memnew(ToolButton);
  495. hbc_button_search->add_child(find_next);
  496. find_next->set_focus_mode(FOCUS_NONE);
  497. find_next->connect("pressed", callable_mp(this, &FindReplaceBar::search_next));
  498. case_sensitive = memnew(CheckBox);
  499. hbc_option_search->add_child(case_sensitive);
  500. case_sensitive->set_text(TTR("Match Case"));
  501. case_sensitive->set_focus_mode(FOCUS_NONE);
  502. case_sensitive->connect("toggled", callable_mp(this, &FindReplaceBar::_search_options_changed));
  503. whole_words = memnew(CheckBox);
  504. hbc_option_search->add_child(whole_words);
  505. whole_words->set_text(TTR("Whole Words"));
  506. whole_words->set_focus_mode(FOCUS_NONE);
  507. whole_words->connect("toggled", callable_mp(this, &FindReplaceBar::_search_options_changed));
  508. // replace toolbar
  509. replace_text = memnew(LineEdit);
  510. vbc_lineedit->add_child(replace_text);
  511. replace_text->set_custom_minimum_size(Size2(100 * EDSCALE, 0));
  512. replace_text->connect("text_entered", callable_mp(this, &FindReplaceBar::_replace_text_entered));
  513. replace = memnew(Button);
  514. hbc_button_replace->add_child(replace);
  515. replace->set_text(TTR("Replace"));
  516. replace->connect("pressed", callable_mp(this, &FindReplaceBar::_replace));
  517. replace_all = memnew(Button);
  518. hbc_button_replace->add_child(replace_all);
  519. replace_all->set_text(TTR("Replace All"));
  520. replace_all->connect("pressed", callable_mp(this, &FindReplaceBar::_replace_all));
  521. selection_only = memnew(CheckBox);
  522. hbc_option_replace->add_child(selection_only);
  523. selection_only->set_text(TTR("Selection Only"));
  524. selection_only->set_focus_mode(FOCUS_NONE);
  525. selection_only->connect("toggled", callable_mp(this, &FindReplaceBar::_search_options_changed));
  526. hide_button = memnew(TextureButton);
  527. add_child(hide_button);
  528. hide_button->set_focus_mode(FOCUS_NONE);
  529. hide_button->connect("pressed", callable_mp(this, &FindReplaceBar::_hide_bar));
  530. hide_button->set_v_size_flags(SIZE_SHRINK_CENTER);
  531. }
  532. /*** CODE EDITOR ****/
  533. // This function should be used to handle shortcuts that could otherwise
  534. // be handled too late if they weren't handled here.
  535. void CodeTextEditor::_input(const Ref<InputEvent> &event) {
  536. const Ref<InputEventKey> key_event = event;
  537. if (!key_event.is_valid() || !key_event->is_pressed() || !text_editor->has_focus()) {
  538. return;
  539. }
  540. if (ED_IS_SHORTCUT("script_text_editor/move_up", key_event)) {
  541. move_lines_up();
  542. accept_event();
  543. return;
  544. }
  545. if (ED_IS_SHORTCUT("script_text_editor/move_down", key_event)) {
  546. move_lines_down();
  547. accept_event();
  548. return;
  549. }
  550. if (ED_IS_SHORTCUT("script_text_editor/delete_line", key_event)) {
  551. delete_lines();
  552. accept_event();
  553. return;
  554. }
  555. if (ED_IS_SHORTCUT("script_text_editor/clone_down", key_event)) {
  556. clone_lines_down();
  557. accept_event();
  558. return;
  559. }
  560. }
  561. void CodeTextEditor::_text_editor_gui_input(const Ref<InputEvent> &p_event) {
  562. Ref<InputEventMouseButton> mb = p_event;
  563. if (mb.is_valid()) {
  564. if (mb->is_pressed() && mb->get_command()) {
  565. if (mb->get_button_index() == BUTTON_WHEEL_UP) {
  566. _zoom_in();
  567. } else if (mb->get_button_index() == BUTTON_WHEEL_DOWN) {
  568. _zoom_out();
  569. }
  570. }
  571. }
  572. Ref<InputEventMagnifyGesture> magnify_gesture = p_event;
  573. if (magnify_gesture.is_valid()) {
  574. Ref<DynamicFont> font = text_editor->get_theme_font("font");
  575. if (font.is_valid()) {
  576. if (font->get_size() != (int)font_size) {
  577. font_size = font->get_size();
  578. }
  579. font_size *= powf(magnify_gesture->get_factor(), 0.25);
  580. _add_font_size((int)font_size - font->get_size());
  581. }
  582. return;
  583. }
  584. Ref<InputEventKey> k = p_event;
  585. if (k.is_valid()) {
  586. if (k->is_pressed()) {
  587. if (ED_IS_SHORTCUT("script_editor/zoom_in", p_event)) {
  588. _zoom_in();
  589. }
  590. if (ED_IS_SHORTCUT("script_editor/zoom_out", p_event)) {
  591. _zoom_out();
  592. }
  593. if (ED_IS_SHORTCUT("script_editor/reset_zoom", p_event)) {
  594. _reset_zoom();
  595. }
  596. }
  597. }
  598. }
  599. void CodeTextEditor::_zoom_in() {
  600. font_resize_val += MAX(EDSCALE, 1.0f);
  601. _zoom_changed();
  602. }
  603. void CodeTextEditor::_zoom_out() {
  604. font_resize_val -= MAX(EDSCALE, 1.0f);
  605. _zoom_changed();
  606. }
  607. void CodeTextEditor::_zoom_changed() {
  608. if (font_resize_timer->get_time_left() == 0) {
  609. font_resize_timer->start();
  610. }
  611. }
  612. void CodeTextEditor::_reset_zoom() {
  613. Ref<DynamicFont> font = text_editor->get_theme_font("font"); // Reset source font size to default.
  614. if (font.is_valid()) {
  615. EditorSettings::get_singleton()->set("interface/editor/code_font_size", 14);
  616. font->set_size(14);
  617. }
  618. }
  619. void CodeTextEditor::_line_col_changed() {
  620. String line = text_editor->get_line(text_editor->cursor_get_line());
  621. int positional_column = 0;
  622. for (int i = 0; i < text_editor->cursor_get_column(); i++) {
  623. if (line[i] == '\t') {
  624. positional_column += text_editor->get_indent_size(); //tab size
  625. } else {
  626. positional_column += 1;
  627. }
  628. }
  629. StringBuilder sb;
  630. sb.append("(");
  631. sb.append(itos(text_editor->cursor_get_line() + 1).lpad(3));
  632. sb.append(",");
  633. sb.append(itos(positional_column + 1).lpad(3));
  634. sb.append(")");
  635. line_and_col_txt->set_text(sb.as_string());
  636. }
  637. void CodeTextEditor::_text_changed() {
  638. if (text_editor->is_insert_text_operation()) {
  639. code_complete_timer->start();
  640. }
  641. idle->start();
  642. }
  643. void CodeTextEditor::_code_complete_timer_timeout() {
  644. if (!is_visible_in_tree()) {
  645. return;
  646. }
  647. text_editor->query_code_comple();
  648. }
  649. void CodeTextEditor::_complete_request() {
  650. List<ScriptCodeCompletionOption> entries;
  651. String ctext = text_editor->get_text_for_completion();
  652. _code_complete_script(ctext, &entries);
  653. bool forced = false;
  654. if (code_complete_func) {
  655. code_complete_func(code_complete_ud, ctext, &entries, forced);
  656. }
  657. if (entries.size() == 0) {
  658. return;
  659. }
  660. for (List<ScriptCodeCompletionOption>::Element *E = entries.front(); E; E = E->next()) {
  661. E->get().icon = _get_completion_icon(E->get());
  662. }
  663. text_editor->code_complete(entries, forced);
  664. }
  665. Ref<Texture2D> CodeTextEditor::_get_completion_icon(const ScriptCodeCompletionOption &p_option) {
  666. Ref<Texture2D> tex;
  667. switch (p_option.kind) {
  668. case ScriptCodeCompletionOption::KIND_CLASS: {
  669. if (has_theme_icon(p_option.display, "EditorIcons")) {
  670. tex = get_theme_icon(p_option.display, "EditorIcons");
  671. } else {
  672. tex = get_theme_icon("Object", "EditorIcons");
  673. }
  674. } break;
  675. case ScriptCodeCompletionOption::KIND_ENUM:
  676. tex = get_theme_icon("Enum", "EditorIcons");
  677. break;
  678. case ScriptCodeCompletionOption::KIND_FILE_PATH:
  679. tex = get_theme_icon("File", "EditorIcons");
  680. break;
  681. case ScriptCodeCompletionOption::KIND_NODE_PATH:
  682. tex = get_theme_icon("NodePath", "EditorIcons");
  683. break;
  684. case ScriptCodeCompletionOption::KIND_VARIABLE:
  685. tex = get_theme_icon("Variant", "EditorIcons");
  686. break;
  687. case ScriptCodeCompletionOption::KIND_CONSTANT:
  688. tex = get_theme_icon("MemberConstant", "EditorIcons");
  689. break;
  690. case ScriptCodeCompletionOption::KIND_MEMBER:
  691. tex = get_theme_icon("MemberProperty", "EditorIcons");
  692. break;
  693. case ScriptCodeCompletionOption::KIND_SIGNAL:
  694. tex = get_theme_icon("MemberSignal", "EditorIcons");
  695. break;
  696. case ScriptCodeCompletionOption::KIND_FUNCTION:
  697. tex = get_theme_icon("MemberMethod", "EditorIcons");
  698. break;
  699. case ScriptCodeCompletionOption::KIND_PLAIN_TEXT:
  700. tex = get_theme_icon("CubeMesh", "EditorIcons");
  701. break;
  702. default:
  703. tex = get_theme_icon("String", "EditorIcons");
  704. break;
  705. }
  706. return tex;
  707. }
  708. void CodeTextEditor::_font_resize_timeout() {
  709. if (_add_font_size(font_resize_val)) {
  710. font_resize_val = 0;
  711. }
  712. }
  713. bool CodeTextEditor::_add_font_size(int p_delta) {
  714. Ref<DynamicFont> font = text_editor->get_theme_font("font");
  715. if (font.is_valid()) {
  716. int new_size = CLAMP(font->get_size() + p_delta, 8 * EDSCALE, 96 * EDSCALE);
  717. if (new_size != font->get_size()) {
  718. EditorSettings::get_singleton()->set("interface/editor/code_font_size", new_size / EDSCALE);
  719. font->set_size(new_size);
  720. }
  721. return true;
  722. } else {
  723. return false;
  724. }
  725. }
  726. void CodeTextEditor::update_editor_settings() {
  727. text_editor->set_syntax_coloring(EditorSettings::get_singleton()->get("text_editor/highlighting/syntax_highlighting"));
  728. text_editor->set_highlight_all_occurrences(EditorSettings::get_singleton()->get("text_editor/highlighting/highlight_all_occurrences"));
  729. text_editor->set_highlight_current_line(EditorSettings::get_singleton()->get("text_editor/highlighting/highlight_current_line"));
  730. text_editor->set_indent_using_spaces(EditorSettings::get_singleton()->get("text_editor/indent/type"));
  731. text_editor->set_indent_size(EditorSettings::get_singleton()->get("text_editor/indent/size"));
  732. text_editor->set_auto_indent(EditorSettings::get_singleton()->get("text_editor/indent/auto_indent"));
  733. text_editor->set_draw_tabs(EditorSettings::get_singleton()->get("text_editor/indent/draw_tabs"));
  734. text_editor->set_draw_spaces(EditorSettings::get_singleton()->get("text_editor/indent/draw_spaces"));
  735. text_editor->set_smooth_scroll_enabled(EditorSettings::get_singleton()->get("text_editor/navigation/smooth_scrolling"));
  736. text_editor->set_v_scroll_speed(EditorSettings::get_singleton()->get("text_editor/navigation/v_scroll_speed"));
  737. text_editor->set_draw_minimap(EditorSettings::get_singleton()->get("text_editor/navigation/show_minimap"));
  738. text_editor->set_minimap_width((int)EditorSettings::get_singleton()->get("text_editor/navigation/minimap_width") * EDSCALE);
  739. text_editor->set_show_line_numbers(EditorSettings::get_singleton()->get("text_editor/appearance/show_line_numbers"));
  740. text_editor->set_line_numbers_zero_padded(EditorSettings::get_singleton()->get("text_editor/appearance/line_numbers_zero_padded"));
  741. text_editor->set_bookmark_gutter_enabled(EditorSettings::get_singleton()->get("text_editor/appearance/show_bookmark_gutter"));
  742. text_editor->set_breakpoint_gutter_enabled(EditorSettings::get_singleton()->get("text_editor/appearance/show_breakpoint_gutter"));
  743. text_editor->set_draw_info_gutter(EditorSettings::get_singleton()->get("text_editor/appearance/show_info_gutter"));
  744. text_editor->set_hiding_enabled(EditorSettings::get_singleton()->get("text_editor/appearance/code_folding"));
  745. text_editor->set_draw_fold_gutter(EditorSettings::get_singleton()->get("text_editor/appearance/code_folding"));
  746. text_editor->set_wrap_enabled(EditorSettings::get_singleton()->get("text_editor/appearance/word_wrap"));
  747. text_editor->set_show_line_length_guidelines(EditorSettings::get_singleton()->get("text_editor/appearance/show_line_length_guidelines"));
  748. text_editor->set_line_length_guideline_soft_column(EditorSettings::get_singleton()->get("text_editor/appearance/line_length_guideline_soft_column"));
  749. text_editor->set_line_length_guideline_hard_column(EditorSettings::get_singleton()->get("text_editor/appearance/line_length_guideline_hard_column"));
  750. text_editor->set_scroll_pass_end_of_file(EditorSettings::get_singleton()->get("text_editor/cursor/scroll_past_end_of_file"));
  751. text_editor->cursor_set_block_mode(EditorSettings::get_singleton()->get("text_editor/cursor/block_caret"));
  752. text_editor->cursor_set_blink_enabled(EditorSettings::get_singleton()->get("text_editor/cursor/caret_blink"));
  753. text_editor->cursor_set_blink_speed(EditorSettings::get_singleton()->get("text_editor/cursor/caret_blink_speed"));
  754. text_editor->set_auto_brace_completion(EditorSettings::get_singleton()->get("text_editor/completion/auto_brace_complete"));
  755. }
  756. void CodeTextEditor::trim_trailing_whitespace() {
  757. bool trimed_whitespace = false;
  758. for (int i = 0; i < text_editor->get_line_count(); i++) {
  759. String line = text_editor->get_line(i);
  760. if (line.ends_with(" ") || line.ends_with("\t")) {
  761. if (!trimed_whitespace) {
  762. text_editor->begin_complex_operation();
  763. trimed_whitespace = true;
  764. }
  765. int end = 0;
  766. for (int j = line.length() - 1; j > -1; j--) {
  767. if (line[j] != ' ' && line[j] != '\t') {
  768. end = j + 1;
  769. break;
  770. }
  771. }
  772. text_editor->set_line(i, line.substr(0, end));
  773. }
  774. }
  775. if (trimed_whitespace) {
  776. text_editor->end_complex_operation();
  777. text_editor->update();
  778. }
  779. }
  780. void CodeTextEditor::insert_final_newline() {
  781. int final_line = text_editor->get_line_count() - 1;
  782. String line = text_editor->get_line(final_line);
  783. //length 0 means it's already an empty line,
  784. //no need to add a newline
  785. if (line.length() > 0 && !line.ends_with("\n")) {
  786. text_editor->begin_complex_operation();
  787. line += "\n";
  788. text_editor->set_line(final_line, line);
  789. text_editor->end_complex_operation();
  790. text_editor->update();
  791. }
  792. }
  793. void CodeTextEditor::convert_indent_to_spaces() {
  794. int indent_size = EditorSettings::get_singleton()->get("text_editor/indent/size");
  795. String indent = "";
  796. for (int i = 0; i < indent_size; i++) {
  797. indent += " ";
  798. }
  799. int cursor_line = text_editor->cursor_get_line();
  800. int cursor_column = text_editor->cursor_get_column();
  801. bool changed_indentation = false;
  802. for (int i = 0; i < text_editor->get_line_count(); i++) {
  803. String line = text_editor->get_line(i);
  804. if (line.length() <= 0) {
  805. continue;
  806. }
  807. int j = 0;
  808. while (j < line.length() && (line[j] == ' ' || line[j] == '\t')) {
  809. if (line[j] == '\t') {
  810. if (!changed_indentation) {
  811. text_editor->begin_complex_operation();
  812. changed_indentation = true;
  813. }
  814. if (cursor_line == i && cursor_column > j) {
  815. cursor_column += indent_size - 1;
  816. }
  817. line = line.left(j) + indent + line.right(j + 1);
  818. }
  819. j++;
  820. }
  821. if (changed_indentation) {
  822. text_editor->set_line(i, line);
  823. }
  824. }
  825. if (changed_indentation) {
  826. text_editor->cursor_set_column(cursor_column);
  827. text_editor->end_complex_operation();
  828. text_editor->update();
  829. }
  830. }
  831. void CodeTextEditor::convert_indent_to_tabs() {
  832. int indent_size = EditorSettings::get_singleton()->get("text_editor/indent/size");
  833. indent_size -= 1;
  834. int cursor_line = text_editor->cursor_get_line();
  835. int cursor_column = text_editor->cursor_get_column();
  836. bool changed_indentation = false;
  837. for (int i = 0; i < text_editor->get_line_count(); i++) {
  838. String line = text_editor->get_line(i);
  839. if (line.length() <= 0) {
  840. continue;
  841. }
  842. int j = 0;
  843. int space_count = -1;
  844. while (j < line.length() && (line[j] == ' ' || line[j] == '\t')) {
  845. if (line[j] != '\t') {
  846. space_count++;
  847. if (space_count == indent_size) {
  848. if (!changed_indentation) {
  849. text_editor->begin_complex_operation();
  850. changed_indentation = true;
  851. }
  852. if (cursor_line == i && cursor_column > j) {
  853. cursor_column -= indent_size;
  854. }
  855. line = line.left(j - indent_size) + "\t" + line.right(j + 1);
  856. j = 0;
  857. space_count = -1;
  858. }
  859. } else {
  860. space_count = -1;
  861. }
  862. j++;
  863. }
  864. if (changed_indentation) {
  865. text_editor->set_line(i, line);
  866. }
  867. }
  868. if (changed_indentation) {
  869. text_editor->cursor_set_column(cursor_column);
  870. text_editor->end_complex_operation();
  871. text_editor->update();
  872. }
  873. }
  874. void CodeTextEditor::convert_case(CaseStyle p_case) {
  875. if (!text_editor->is_selection_active()) {
  876. return;
  877. }
  878. text_editor->begin_complex_operation();
  879. int begin = text_editor->get_selection_from_line();
  880. int end = text_editor->get_selection_to_line();
  881. int begin_col = text_editor->get_selection_from_column();
  882. int end_col = text_editor->get_selection_to_column();
  883. for (int i = begin; i <= end; i++) {
  884. int len = text_editor->get_line(i).length();
  885. if (i == end) {
  886. len = end_col;
  887. }
  888. if (i == begin) {
  889. len -= begin_col;
  890. }
  891. String new_line = text_editor->get_line(i).substr(i == begin ? begin_col : 0, len);
  892. switch (p_case) {
  893. case UPPER: {
  894. new_line = new_line.to_upper();
  895. } break;
  896. case LOWER: {
  897. new_line = new_line.to_lower();
  898. } break;
  899. case CAPITALIZE: {
  900. new_line = new_line.capitalize();
  901. } break;
  902. }
  903. if (i == begin) {
  904. new_line = text_editor->get_line(i).left(begin_col) + new_line;
  905. }
  906. if (i == end) {
  907. new_line = new_line + text_editor->get_line(i).right(end_col);
  908. }
  909. text_editor->set_line(i, new_line);
  910. }
  911. text_editor->end_complex_operation();
  912. }
  913. void CodeTextEditor::move_lines_up() {
  914. text_editor->begin_complex_operation();
  915. if (text_editor->is_selection_active()) {
  916. int from_line = text_editor->get_selection_from_line();
  917. int from_col = text_editor->get_selection_from_column();
  918. int to_line = text_editor->get_selection_to_line();
  919. int to_column = text_editor->get_selection_to_column();
  920. for (int i = from_line; i <= to_line; i++) {
  921. int line_id = i;
  922. int next_id = i - 1;
  923. if (line_id == 0 || next_id < 0) {
  924. return;
  925. }
  926. text_editor->unfold_line(line_id);
  927. text_editor->unfold_line(next_id);
  928. text_editor->swap_lines(line_id, next_id);
  929. text_editor->cursor_set_line(next_id);
  930. }
  931. int from_line_up = from_line > 0 ? from_line - 1 : from_line;
  932. int to_line_up = to_line > 0 ? to_line - 1 : to_line;
  933. text_editor->select(from_line_up, from_col, to_line_up, to_column);
  934. } else {
  935. int line_id = text_editor->cursor_get_line();
  936. int next_id = line_id - 1;
  937. if (line_id == 0 || next_id < 0) {
  938. return;
  939. }
  940. text_editor->unfold_line(line_id);
  941. text_editor->unfold_line(next_id);
  942. text_editor->swap_lines(line_id, next_id);
  943. text_editor->cursor_set_line(next_id);
  944. }
  945. text_editor->end_complex_operation();
  946. text_editor->update();
  947. }
  948. void CodeTextEditor::move_lines_down() {
  949. text_editor->begin_complex_operation();
  950. if (text_editor->is_selection_active()) {
  951. int from_line = text_editor->get_selection_from_line();
  952. int from_col = text_editor->get_selection_from_column();
  953. int to_line = text_editor->get_selection_to_line();
  954. int to_column = text_editor->get_selection_to_column();
  955. for (int i = to_line; i >= from_line; i--) {
  956. int line_id = i;
  957. int next_id = i + 1;
  958. if (line_id == text_editor->get_line_count() - 1 || next_id > text_editor->get_line_count()) {
  959. return;
  960. }
  961. text_editor->unfold_line(line_id);
  962. text_editor->unfold_line(next_id);
  963. text_editor->swap_lines(line_id, next_id);
  964. text_editor->cursor_set_line(next_id);
  965. }
  966. int from_line_down = from_line < text_editor->get_line_count() ? from_line + 1 : from_line;
  967. int to_line_down = to_line < text_editor->get_line_count() ? to_line + 1 : to_line;
  968. text_editor->select(from_line_down, from_col, to_line_down, to_column);
  969. } else {
  970. int line_id = text_editor->cursor_get_line();
  971. int next_id = line_id + 1;
  972. if (line_id == text_editor->get_line_count() - 1 || next_id > text_editor->get_line_count()) {
  973. return;
  974. }
  975. text_editor->unfold_line(line_id);
  976. text_editor->unfold_line(next_id);
  977. text_editor->swap_lines(line_id, next_id);
  978. text_editor->cursor_set_line(next_id);
  979. }
  980. text_editor->end_complex_operation();
  981. text_editor->update();
  982. }
  983. void CodeTextEditor::_delete_line(int p_line) {
  984. // this is currently intended to be called within delete_lines()
  985. // so `begin_complex_operation` is omitted here
  986. text_editor->set_line(p_line, "");
  987. if (p_line == 0 && text_editor->get_line_count() > 1) {
  988. text_editor->cursor_set_line(1);
  989. text_editor->cursor_set_column(0);
  990. }
  991. text_editor->backspace_at_cursor();
  992. text_editor->unfold_line(p_line);
  993. text_editor->cursor_set_line(p_line);
  994. }
  995. void CodeTextEditor::delete_lines() {
  996. text_editor->begin_complex_operation();
  997. if (text_editor->is_selection_active()) {
  998. int to_line = text_editor->get_selection_to_line();
  999. int from_line = text_editor->get_selection_from_line();
  1000. int count = Math::abs(to_line - from_line) + 1;
  1001. text_editor->cursor_set_line(from_line, false);
  1002. for (int i = 0; i < count; i++) {
  1003. _delete_line(from_line);
  1004. }
  1005. text_editor->deselect();
  1006. } else {
  1007. _delete_line(text_editor->cursor_get_line());
  1008. }
  1009. text_editor->end_complex_operation();
  1010. }
  1011. void CodeTextEditor::clone_lines_down() {
  1012. const int cursor_column = text_editor->cursor_get_column();
  1013. int from_line = text_editor->cursor_get_line();
  1014. int to_line = text_editor->cursor_get_line();
  1015. int from_column = 0;
  1016. int to_column = 0;
  1017. int cursor_new_line = to_line + 1;
  1018. int cursor_new_column = text_editor->cursor_get_column();
  1019. String new_text = "\n" + text_editor->get_line(from_line);
  1020. bool selection_active = false;
  1021. text_editor->cursor_set_column(text_editor->get_line(from_line).length());
  1022. if (text_editor->is_selection_active()) {
  1023. from_column = text_editor->get_selection_from_column();
  1024. to_column = text_editor->get_selection_to_column();
  1025. from_line = text_editor->get_selection_from_line();
  1026. to_line = text_editor->get_selection_to_line();
  1027. cursor_new_line = to_line + text_editor->cursor_get_line() - from_line;
  1028. cursor_new_column = to_column == cursor_column ? 2 * to_column - from_column : to_column;
  1029. new_text = text_editor->get_selection_text();
  1030. selection_active = true;
  1031. text_editor->cursor_set_line(to_line);
  1032. text_editor->cursor_set_column(to_column);
  1033. }
  1034. text_editor->begin_complex_operation();
  1035. for (int i = from_line; i <= to_line; i++) {
  1036. text_editor->unfold_line(i);
  1037. }
  1038. text_editor->deselect();
  1039. text_editor->insert_text_at_cursor(new_text);
  1040. text_editor->cursor_set_line(cursor_new_line);
  1041. text_editor->cursor_set_column(cursor_new_column);
  1042. if (selection_active) {
  1043. text_editor->select(to_line, to_column, 2 * to_line - from_line, to_line == from_line ? 2 * to_column - from_column : to_column);
  1044. }
  1045. text_editor->end_complex_operation();
  1046. text_editor->update();
  1047. }
  1048. void CodeTextEditor::toggle_inline_comment(const String &delimiter) {
  1049. text_editor->begin_complex_operation();
  1050. if (text_editor->is_selection_active()) {
  1051. int begin = text_editor->get_selection_from_line();
  1052. int end = text_editor->get_selection_to_line();
  1053. // End of selection ends on the first column of the last line, ignore it.
  1054. if (text_editor->get_selection_to_column() == 0) {
  1055. end -= 1;
  1056. }
  1057. int col_to = text_editor->get_selection_to_column();
  1058. int cursor_pos = text_editor->cursor_get_column();
  1059. // Check if all lines in the selected block are commented.
  1060. bool is_commented = true;
  1061. for (int i = begin; i <= end; i++) {
  1062. if (!text_editor->get_line(i).begins_with(delimiter)) {
  1063. is_commented = false;
  1064. break;
  1065. }
  1066. }
  1067. for (int i = begin; i <= end; i++) {
  1068. String line_text = text_editor->get_line(i);
  1069. if (line_text.strip_edges().empty()) {
  1070. line_text = delimiter;
  1071. } else {
  1072. if (is_commented) {
  1073. line_text = line_text.substr(delimiter.length(), line_text.length());
  1074. } else {
  1075. line_text = delimiter + line_text;
  1076. }
  1077. }
  1078. text_editor->set_line(i, line_text);
  1079. }
  1080. // Adjust selection & cursor position.
  1081. int offset = (is_commented ? -1 : 1) * delimiter.length();
  1082. int col_from = text_editor->get_selection_from_column() > 0 ? text_editor->get_selection_from_column() + offset : 0;
  1083. if (is_commented && text_editor->cursor_get_column() == text_editor->get_line(text_editor->cursor_get_line()).length() + 1) {
  1084. cursor_pos += 1;
  1085. }
  1086. if (text_editor->get_selection_to_column() != 0 && col_to != text_editor->get_line(text_editor->get_selection_to_line()).length() + 1) {
  1087. col_to += offset;
  1088. }
  1089. if (text_editor->cursor_get_column() != 0) {
  1090. cursor_pos += offset;
  1091. }
  1092. text_editor->select(begin, col_from, text_editor->get_selection_to_line(), col_to);
  1093. text_editor->cursor_set_column(cursor_pos);
  1094. } else {
  1095. int begin = text_editor->cursor_get_line();
  1096. String line_text = text_editor->get_line(begin);
  1097. int delimiter_length = delimiter.length();
  1098. int col = text_editor->cursor_get_column();
  1099. if (line_text.begins_with(delimiter)) {
  1100. line_text = line_text.substr(delimiter_length, line_text.length());
  1101. col -= delimiter_length;
  1102. } else {
  1103. line_text = delimiter + line_text;
  1104. col += delimiter_length;
  1105. }
  1106. text_editor->set_line(begin, line_text);
  1107. text_editor->cursor_set_column(col);
  1108. }
  1109. text_editor->end_complex_operation();
  1110. text_editor->update();
  1111. }
  1112. void CodeTextEditor::goto_line(int p_line) {
  1113. text_editor->deselect();
  1114. text_editor->unfold_line(p_line);
  1115. text_editor->call_deferred("cursor_set_line", p_line);
  1116. }
  1117. void CodeTextEditor::goto_line_selection(int p_line, int p_begin, int p_end) {
  1118. text_editor->unfold_line(p_line);
  1119. text_editor->call_deferred("cursor_set_line", p_line);
  1120. text_editor->call_deferred("cursor_set_column", p_begin);
  1121. text_editor->select(p_line, p_begin, p_line, p_end);
  1122. }
  1123. void CodeTextEditor::goto_line_centered(int p_line) {
  1124. goto_line(p_line);
  1125. text_editor->call_deferred("center_viewport_to_cursor");
  1126. }
  1127. void CodeTextEditor::set_executing_line(int p_line) {
  1128. text_editor->set_executing_line(p_line);
  1129. }
  1130. void CodeTextEditor::clear_executing_line() {
  1131. text_editor->clear_executing_line();
  1132. }
  1133. Variant CodeTextEditor::get_edit_state() {
  1134. Dictionary state;
  1135. state["scroll_position"] = text_editor->get_v_scroll();
  1136. state["h_scroll_position"] = text_editor->get_h_scroll();
  1137. state["column"] = text_editor->cursor_get_column();
  1138. state["row"] = text_editor->cursor_get_line();
  1139. state["selection"] = get_text_edit()->is_selection_active();
  1140. if (get_text_edit()->is_selection_active()) {
  1141. state["selection_from_line"] = text_editor->get_selection_from_line();
  1142. state["selection_from_column"] = text_editor->get_selection_from_column();
  1143. state["selection_to_line"] = text_editor->get_selection_to_line();
  1144. state["selection_to_column"] = text_editor->get_selection_to_column();
  1145. }
  1146. state["folded_lines"] = text_editor->get_folded_lines();
  1147. state["breakpoints"] = text_editor->get_breakpoints_array();
  1148. state["bookmarks"] = text_editor->get_bookmarks_array();
  1149. state["syntax_highlighter"] = TTR("Standard");
  1150. SyntaxHighlighter *syntax_highlighter = text_editor->_get_syntax_highlighting();
  1151. if (syntax_highlighter) {
  1152. state["syntax_highlighter"] = syntax_highlighter->get_name();
  1153. }
  1154. return state;
  1155. }
  1156. void CodeTextEditor::set_edit_state(const Variant &p_state) {
  1157. Dictionary state = p_state;
  1158. /* update the row first as it sets the column to 0 */
  1159. text_editor->cursor_set_line(state["row"]);
  1160. text_editor->cursor_set_column(state["column"]);
  1161. text_editor->set_v_scroll(state["scroll_position"]);
  1162. text_editor->set_h_scroll(state["h_scroll_position"]);
  1163. if (state.has("selection")) {
  1164. text_editor->select(state["selection_from_line"], state["selection_from_column"], state["selection_to_line"], state["selection_to_column"]);
  1165. }
  1166. if (state.has("folded_lines")) {
  1167. Vector<int> folded_lines = state["folded_lines"];
  1168. for (int i = 0; i < folded_lines.size(); i++) {
  1169. text_editor->fold_line(folded_lines[i]);
  1170. }
  1171. }
  1172. if (state.has("breakpoints")) {
  1173. Array breakpoints = state["breakpoints"];
  1174. for (int i = 0; i < breakpoints.size(); i++) {
  1175. text_editor->set_line_as_breakpoint(breakpoints[i], true);
  1176. }
  1177. }
  1178. if (state.has("bookmarks")) {
  1179. Array bookmarks = state["bookmarks"];
  1180. for (int i = 0; i < bookmarks.size(); i++) {
  1181. text_editor->set_line_as_bookmark(bookmarks[i], true);
  1182. }
  1183. }
  1184. text_editor->grab_focus();
  1185. }
  1186. void CodeTextEditor::set_error(const String &p_error) {
  1187. error->set_text(p_error);
  1188. if (p_error != "") {
  1189. error->set_default_cursor_shape(CURSOR_POINTING_HAND);
  1190. } else {
  1191. error->set_default_cursor_shape(CURSOR_ARROW);
  1192. }
  1193. }
  1194. void CodeTextEditor::set_error_pos(int p_line, int p_column) {
  1195. error_line = p_line;
  1196. error_column = p_column;
  1197. }
  1198. void CodeTextEditor::goto_error() {
  1199. if (error->get_text() != "") {
  1200. text_editor->cursor_set_line(error_line);
  1201. text_editor->cursor_set_column(error_column);
  1202. text_editor->center_viewport_to_cursor();
  1203. }
  1204. }
  1205. void CodeTextEditor::_update_font() {
  1206. text_editor->add_theme_font_override("font", get_theme_font("source", "EditorFonts"));
  1207. error->add_theme_font_override("font", get_theme_font("status_source", "EditorFonts"));
  1208. error->add_theme_color_override("font_color", get_theme_color("error_color", "Editor"));
  1209. Ref<Font> status_bar_font = get_theme_font("status_source", "EditorFonts");
  1210. error->add_theme_font_override("font", status_bar_font);
  1211. int count = status_bar->get_child_count();
  1212. for (int i = 0; i < count; i++) {
  1213. Control *n = Object::cast_to<Control>(status_bar->get_child(i));
  1214. if (n) {
  1215. n->add_theme_font_override("font", status_bar_font);
  1216. }
  1217. }
  1218. }
  1219. void CodeTextEditor::_on_settings_change() {
  1220. _update_font();
  1221. font_size = EditorSettings::get_singleton()->get("interface/editor/code_font_size");
  1222. // Auto brace completion.
  1223. text_editor->set_auto_brace_completion(
  1224. EDITOR_GET("text_editor/completion/auto_brace_complete"));
  1225. code_complete_timer->set_wait_time(
  1226. EDITOR_GET("text_editor/completion/code_complete_delay"));
  1227. // Call hint settings.
  1228. text_editor->set_callhint_settings(
  1229. EDITOR_GET("text_editor/completion/put_callhint_tooltip_below_current_line"),
  1230. EDITOR_GET("text_editor/completion/callhint_tooltip_offset"));
  1231. idle->set_wait_time(EDITOR_GET("text_editor/completion/idle_parse_delay"));
  1232. }
  1233. void CodeTextEditor::_text_changed_idle_timeout() {
  1234. _validate_script();
  1235. emit_signal("validate_script");
  1236. }
  1237. void CodeTextEditor::validate_script() {
  1238. idle->start();
  1239. }
  1240. void CodeTextEditor::_warning_label_gui_input(const Ref<InputEvent> &p_event) {
  1241. Ref<InputEventMouseButton> mb = p_event;
  1242. if (mb.is_valid() && mb->is_pressed() && mb->get_button_index() == BUTTON_LEFT) {
  1243. _warning_button_pressed();
  1244. }
  1245. }
  1246. void CodeTextEditor::_warning_button_pressed() {
  1247. _set_show_warnings_panel(!is_warnings_panel_opened);
  1248. }
  1249. void CodeTextEditor::_set_show_warnings_panel(bool p_show) {
  1250. is_warnings_panel_opened = p_show;
  1251. emit_signal("show_warnings_panel", p_show);
  1252. }
  1253. void CodeTextEditor::_toggle_scripts_pressed() {
  1254. toggle_scripts_button->set_icon(ScriptEditor::get_singleton()->toggle_scripts_panel() ? get_theme_icon("Back", "EditorIcons") : get_theme_icon("Forward", "EditorIcons"));
  1255. }
  1256. void CodeTextEditor::_error_pressed(const Ref<InputEvent> &p_event) {
  1257. Ref<InputEventMouseButton> mb = p_event;
  1258. if (mb.is_valid() && mb->is_pressed() && mb->get_button_index() == BUTTON_LEFT) {
  1259. emit_signal("error_pressed");
  1260. }
  1261. }
  1262. void CodeTextEditor::_notification(int p_what) {
  1263. switch (p_what) {
  1264. case EditorSettings::NOTIFICATION_EDITOR_SETTINGS_CHANGED: {
  1265. _load_theme_settings();
  1266. emit_signal("load_theme_settings");
  1267. } break;
  1268. case NOTIFICATION_THEME_CHANGED: {
  1269. if (toggle_scripts_button->is_visible()) {
  1270. update_toggle_scripts_button();
  1271. }
  1272. _update_font();
  1273. } break;
  1274. case NOTIFICATION_ENTER_TREE: {
  1275. warning_button->set_icon(get_theme_icon("NodeWarning", "EditorIcons"));
  1276. add_theme_constant_override("separation", 4 * EDSCALE);
  1277. } break;
  1278. case NOTIFICATION_VISIBILITY_CHANGED: {
  1279. if (toggle_scripts_button->is_visible()) {
  1280. update_toggle_scripts_button();
  1281. }
  1282. set_process_input(is_visible_in_tree());
  1283. } break;
  1284. default:
  1285. break;
  1286. }
  1287. }
  1288. void CodeTextEditor::set_warning_nb(int p_warning_nb) {
  1289. warning_count_label->set_text(itos(p_warning_nb));
  1290. warning_count_label->set_visible(p_warning_nb > 0);
  1291. warning_button->set_visible(p_warning_nb > 0);
  1292. if (!p_warning_nb) {
  1293. _set_show_warnings_panel(false);
  1294. }
  1295. }
  1296. void CodeTextEditor::toggle_bookmark() {
  1297. int line = text_editor->cursor_get_line();
  1298. text_editor->set_line_as_bookmark(line, !text_editor->is_line_set_as_bookmark(line));
  1299. }
  1300. void CodeTextEditor::goto_next_bookmark() {
  1301. List<int> bmarks;
  1302. text_editor->get_bookmarks(&bmarks);
  1303. if (bmarks.size() <= 0) {
  1304. return;
  1305. }
  1306. int line = text_editor->cursor_get_line();
  1307. if (line >= bmarks[bmarks.size() - 1]) {
  1308. text_editor->unfold_line(bmarks[0]);
  1309. text_editor->cursor_set_line(bmarks[0]);
  1310. text_editor->center_viewport_to_cursor();
  1311. } else {
  1312. for (List<int>::Element *E = bmarks.front(); E; E = E->next()) {
  1313. int bline = E->get();
  1314. if (bline > line) {
  1315. text_editor->unfold_line(bline);
  1316. text_editor->cursor_set_line(bline);
  1317. text_editor->center_viewport_to_cursor();
  1318. return;
  1319. }
  1320. }
  1321. }
  1322. }
  1323. void CodeTextEditor::goto_prev_bookmark() {
  1324. List<int> bmarks;
  1325. text_editor->get_bookmarks(&bmarks);
  1326. if (bmarks.size() <= 0) {
  1327. return;
  1328. }
  1329. int line = text_editor->cursor_get_line();
  1330. if (line <= bmarks[0]) {
  1331. text_editor->unfold_line(bmarks[bmarks.size() - 1]);
  1332. text_editor->cursor_set_line(bmarks[bmarks.size() - 1]);
  1333. text_editor->center_viewport_to_cursor();
  1334. } else {
  1335. for (List<int>::Element *E = bmarks.back(); E; E = E->prev()) {
  1336. int bline = E->get();
  1337. if (bline < line) {
  1338. text_editor->unfold_line(bline);
  1339. text_editor->cursor_set_line(bline);
  1340. text_editor->center_viewport_to_cursor();
  1341. return;
  1342. }
  1343. }
  1344. }
  1345. }
  1346. void CodeTextEditor::remove_all_bookmarks() {
  1347. List<int> bmarks;
  1348. text_editor->get_bookmarks(&bmarks);
  1349. for (List<int>::Element *E = bmarks.front(); E; E = E->next()) {
  1350. text_editor->set_line_as_bookmark(E->get(), false);
  1351. }
  1352. }
  1353. void CodeTextEditor::_bind_methods() {
  1354. ClassDB::bind_method(D_METHOD("_input"), &CodeTextEditor::_input);
  1355. ADD_SIGNAL(MethodInfo("validate_script"));
  1356. ADD_SIGNAL(MethodInfo("load_theme_settings"));
  1357. ADD_SIGNAL(MethodInfo("show_warnings_panel"));
  1358. ADD_SIGNAL(MethodInfo("error_pressed"));
  1359. }
  1360. void CodeTextEditor::set_code_complete_func(CodeTextEditorCodeCompleteFunc p_code_complete_func, void *p_ud) {
  1361. code_complete_func = p_code_complete_func;
  1362. code_complete_ud = p_ud;
  1363. }
  1364. void CodeTextEditor::show_toggle_scripts_button() {
  1365. toggle_scripts_button->show();
  1366. }
  1367. void CodeTextEditor::update_toggle_scripts_button() {
  1368. toggle_scripts_button->set_icon(ScriptEditor::get_singleton()->is_scripts_panel_toggled() ? get_theme_icon("Back", "EditorIcons") : get_theme_icon("Forward", "EditorIcons"));
  1369. toggle_scripts_button->set_tooltip(TTR("Toggle Scripts Panel") + " (" + ED_GET_SHORTCUT("script_editor/toggle_scripts_panel")->get_as_text() + ")");
  1370. }
  1371. CodeTextEditor::CodeTextEditor() {
  1372. code_complete_func = nullptr;
  1373. ED_SHORTCUT("script_editor/zoom_in", TTR("Zoom In"), KEY_MASK_CMD | KEY_EQUAL);
  1374. ED_SHORTCUT("script_editor/zoom_out", TTR("Zoom Out"), KEY_MASK_CMD | KEY_MINUS);
  1375. ED_SHORTCUT("script_editor/reset_zoom", TTR("Reset Zoom"), KEY_MASK_CMD | KEY_0);
  1376. text_editor = memnew(TextEdit);
  1377. add_child(text_editor);
  1378. text_editor->set_v_size_flags(SIZE_EXPAND_FILL);
  1379. // Added second so it opens at the bottom, so it won't shift the entire text editor when opening.
  1380. find_replace_bar = memnew(FindReplaceBar);
  1381. add_child(find_replace_bar);
  1382. find_replace_bar->set_h_size_flags(SIZE_EXPAND_FILL);
  1383. find_replace_bar->hide();
  1384. find_replace_bar->set_text_edit(text_editor);
  1385. text_editor->set_show_line_numbers(true);
  1386. text_editor->set_brace_matching(true);
  1387. text_editor->set_auto_indent(true);
  1388. status_bar = memnew(HBoxContainer);
  1389. add_child(status_bar);
  1390. status_bar->set_h_size_flags(SIZE_EXPAND_FILL);
  1391. status_bar->set_custom_minimum_size(Size2(0, 24 * EDSCALE)); // Adjust for the height of the warning icon.
  1392. idle = memnew(Timer);
  1393. add_child(idle);
  1394. idle->set_one_shot(true);
  1395. idle->set_wait_time(EDITOR_GET("text_editor/completion/idle_parse_delay"));
  1396. code_complete_timer = memnew(Timer);
  1397. add_child(code_complete_timer);
  1398. code_complete_timer->set_one_shot(true);
  1399. code_complete_timer->set_wait_time(EDITOR_GET("text_editor/completion/code_complete_delay"));
  1400. error_line = 0;
  1401. error_column = 0;
  1402. toggle_scripts_button = memnew(ToolButton);
  1403. toggle_scripts_button->connect("pressed", callable_mp(this, &CodeTextEditor::_toggle_scripts_pressed));
  1404. status_bar->add_child(toggle_scripts_button);
  1405. toggle_scripts_button->hide();
  1406. // Error
  1407. ScrollContainer *scroll = memnew(ScrollContainer);
  1408. scroll->set_h_size_flags(SIZE_EXPAND_FILL);
  1409. scroll->set_v_size_flags(SIZE_EXPAND_FILL);
  1410. scroll->set_enable_v_scroll(false);
  1411. status_bar->add_child(scroll);
  1412. error = memnew(Label);
  1413. scroll->add_child(error);
  1414. error->set_v_size_flags(SIZE_EXPAND | SIZE_SHRINK_CENTER);
  1415. error->set_mouse_filter(MOUSE_FILTER_STOP);
  1416. error->connect("gui_input", callable_mp(this, &CodeTextEditor::_error_pressed));
  1417. find_replace_bar->connect("error", callable_mp(error, &Label::set_text));
  1418. // Warnings
  1419. warning_button = memnew(ToolButton);
  1420. status_bar->add_child(warning_button);
  1421. warning_button->set_v_size_flags(SIZE_EXPAND | SIZE_SHRINK_CENTER);
  1422. warning_button->set_default_cursor_shape(CURSOR_POINTING_HAND);
  1423. warning_button->connect("pressed", callable_mp(this, &CodeTextEditor::_warning_button_pressed));
  1424. warning_button->set_tooltip(TTR("Warnings"));
  1425. warning_count_label = memnew(Label);
  1426. status_bar->add_child(warning_count_label);
  1427. warning_count_label->set_v_size_flags(SIZE_EXPAND | SIZE_SHRINK_CENTER);
  1428. warning_count_label->set_align(Label::ALIGN_RIGHT);
  1429. warning_count_label->set_default_cursor_shape(CURSOR_POINTING_HAND);
  1430. warning_count_label->set_mouse_filter(MOUSE_FILTER_STOP);
  1431. warning_count_label->set_tooltip(TTR("Warnings"));
  1432. warning_count_label->add_theme_color_override("font_color", EditorNode::get_singleton()->get_gui_base()->get_theme_color("warning_color", "Editor"));
  1433. warning_count_label->add_theme_font_override("font", EditorNode::get_singleton()->get_gui_base()->get_theme_font("status_source", "EditorFonts"));
  1434. warning_count_label->connect("gui_input", callable_mp(this, &CodeTextEditor::_warning_label_gui_input));
  1435. is_warnings_panel_opened = false;
  1436. set_warning_nb(0);
  1437. // Line and column
  1438. line_and_col_txt = memnew(Label);
  1439. status_bar->add_child(line_and_col_txt);
  1440. line_and_col_txt->set_v_size_flags(SIZE_EXPAND | SIZE_SHRINK_CENTER);
  1441. line_and_col_txt->add_theme_font_override("font", EditorNode::get_singleton()->get_gui_base()->get_theme_font("status_source", "EditorFonts"));
  1442. line_and_col_txt->set_tooltip(TTR("Line and column numbers."));
  1443. line_and_col_txt->set_mouse_filter(MOUSE_FILTER_STOP);
  1444. text_editor->connect("gui_input", callable_mp(this, &CodeTextEditor::_text_editor_gui_input));
  1445. text_editor->connect("cursor_changed", callable_mp(this, &CodeTextEditor::_line_col_changed));
  1446. text_editor->connect("text_changed", callable_mp(this, &CodeTextEditor::_text_changed));
  1447. text_editor->connect("request_completion", callable_mp(this, &CodeTextEditor::_complete_request));
  1448. Vector<String> cs;
  1449. cs.push_back(".");
  1450. cs.push_back(",");
  1451. cs.push_back("(");
  1452. cs.push_back("=");
  1453. cs.push_back("$");
  1454. text_editor->set_completion(true, cs);
  1455. idle->connect("timeout", callable_mp(this, &CodeTextEditor::_text_changed_idle_timeout));
  1456. code_complete_timer->connect("timeout", callable_mp(this, &CodeTextEditor::_code_complete_timer_timeout));
  1457. font_resize_val = 0;
  1458. font_size = EditorSettings::get_singleton()->get("interface/editor/code_font_size");
  1459. font_resize_timer = memnew(Timer);
  1460. add_child(font_resize_timer);
  1461. font_resize_timer->set_one_shot(true);
  1462. font_resize_timer->set_wait_time(0.07);
  1463. font_resize_timer->connect("timeout", callable_mp(this, &CodeTextEditor::_font_resize_timeout));
  1464. EditorSettings::get_singleton()->connect("settings_changed", callable_mp(this, &CodeTextEditor::_on_settings_change));
  1465. }