code_editor.cpp 56 KB

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