code_editor.cpp 60 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842
  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/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/font.h"
  40. void GotoLineDialog::popup_find_line(CodeEdit *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_offset(SIDE_LEFT, Control::ANCHOR_BEGIN, 8 * EDSCALE);
  62. vbc->set_anchor_and_offset(SIDE_TOP, Control::ANCHOR_BEGIN, 8 * EDSCALE);
  63. vbc->set_anchor_and_offset(SIDE_RIGHT, Control::ANCHOR_END, -8 * EDSCALE);
  64. vbc->set_anchor_and_offset(SIDE_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. line_label = nullptr;
  74. set_hide_on_ok(false);
  75. }
  76. void FindReplaceBar::_notification(int p_what) {
  77. if (p_what == NOTIFICATION_READY) {
  78. find_prev->set_icon(get_theme_icon("MoveUp", "EditorIcons"));
  79. find_next->set_icon(get_theme_icon("MoveDown", "EditorIcons"));
  80. hide_button->set_normal_texture(get_theme_icon("Close", "EditorIcons"));
  81. hide_button->set_hover_texture(get_theme_icon("Close", "EditorIcons"));
  82. hide_button->set_pressed_texture(get_theme_icon("Close", "EditorIcons"));
  83. hide_button->set_custom_minimum_size(hide_button->get_normal_texture()->get_size());
  84. } else if (p_what == NOTIFICATION_VISIBILITY_CHANGED) {
  85. set_process_unhandled_input(is_visible_in_tree());
  86. } else if (p_what == EditorSettings::NOTIFICATION_EDITOR_SETTINGS_CHANGED) {
  87. find_prev->set_icon(get_theme_icon("MoveUp", "EditorIcons"));
  88. find_next->set_icon(get_theme_icon("MoveDown", "EditorIcons"));
  89. hide_button->set_normal_texture(get_theme_icon("Close", "EditorIcons"));
  90. hide_button->set_hover_texture(get_theme_icon("Close", "EditorIcons"));
  91. hide_button->set_pressed_texture(get_theme_icon("Close", "EditorIcons"));
  92. hide_button->set_custom_minimum_size(hide_button->get_normal_texture()->get_size());
  93. } else if (p_what == NOTIFICATION_THEME_CHANGED) {
  94. matches_label->add_theme_color_override("font_color", results_count > 0 ? get_theme_color("font_color", "Label") : get_theme_color("error_color", "Editor"));
  95. }
  96. }
  97. void FindReplaceBar::_unhandled_input(const Ref<InputEvent> &p_event) {
  98. Ref<InputEventKey> k = p_event;
  99. if (!k.is_valid() || !k->is_pressed()) {
  100. return;
  101. }
  102. Control *focus_owner = get_focus_owner();
  103. if (text_editor->has_focus() || (focus_owner && vbc_lineedit->is_a_parent_of(focus_owner))) {
  104. bool accepted = true;
  105. switch (k->get_keycode()) {
  106. case KEY_ESCAPE: {
  107. _hide_bar();
  108. } break;
  109. default: {
  110. accepted = false;
  111. } break;
  112. }
  113. if (accepted) {
  114. accept_event();
  115. }
  116. }
  117. }
  118. bool FindReplaceBar::_search(uint32_t p_flags, int p_from_line, int p_from_col) {
  119. int line, col;
  120. String text = get_search_text();
  121. bool found = text_editor->search(text, p_flags, p_from_line, p_from_col, line, col);
  122. if (found) {
  123. if (!preserve_cursor) {
  124. text_editor->unfold_line(line);
  125. text_editor->cursor_set_line(line, false);
  126. text_editor->cursor_set_column(col + text.length(), false);
  127. text_editor->center_viewport_to_cursor();
  128. text_editor->select(line, col, line, col + text.length());
  129. }
  130. text_editor->set_search_text(text);
  131. text_editor->set_search_flags(p_flags);
  132. text_editor->set_current_search_result(line, col);
  133. result_line = line;
  134. result_col = col;
  135. _update_results_count();
  136. } else {
  137. results_count = 0;
  138. result_line = -1;
  139. result_col = -1;
  140. text_editor->set_search_text("");
  141. text_editor->set_search_flags(p_flags);
  142. text_editor->set_current_search_result(line, col);
  143. }
  144. _update_matches_label();
  145. return found;
  146. }
  147. void FindReplaceBar::_replace() {
  148. bool selection_enabled = text_editor->is_selection_active();
  149. Point2i selection_begin, selection_end;
  150. if (selection_enabled) {
  151. selection_begin = Point2i(text_editor->get_selection_from_line(), text_editor->get_selection_from_column());
  152. selection_end = Point2i(text_editor->get_selection_to_line(), text_editor->get_selection_to_column());
  153. }
  154. String replace_text = get_replace_text();
  155. int search_text_len = get_search_text().length();
  156. text_editor->begin_complex_operation();
  157. if (selection_enabled && is_selection_only()) { // To restrict search_current() to selected region
  158. text_editor->cursor_set_line(selection_begin.width);
  159. text_editor->cursor_set_column(selection_begin.height);
  160. }
  161. if (search_current()) {
  162. text_editor->unfold_line(result_line);
  163. text_editor->select(result_line, result_col, result_line, result_col + search_text_len);
  164. if (selection_enabled && is_selection_only()) {
  165. Point2i match_from(result_line, result_col);
  166. Point2i match_to(result_line, result_col + search_text_len);
  167. if (!(match_from < selection_begin || match_to > selection_end)) {
  168. text_editor->insert_text_at_cursor(replace_text);
  169. if (match_to.x == selection_end.x) { // Adjust selection bounds if necessary
  170. selection_end.y += replace_text.length() - search_text_len;
  171. }
  172. }
  173. } else {
  174. text_editor->insert_text_at_cursor(replace_text);
  175. }
  176. }
  177. text_editor->end_complex_operation();
  178. results_count = -1;
  179. if (selection_enabled && is_selection_only()) {
  180. // Reselect in order to keep 'Replace' restricted to selection
  181. text_editor->select(selection_begin.x, selection_begin.y, selection_end.x, selection_end.y);
  182. } else {
  183. text_editor->deselect();
  184. }
  185. }
  186. void FindReplaceBar::_replace_all() {
  187. text_editor->disconnect("text_changed", callable_mp(this, &FindReplaceBar::_editor_text_changed));
  188. // Line as x so it gets priority in comparison, column as y.
  189. Point2i orig_cursor(text_editor->cursor_get_line(), text_editor->cursor_get_column());
  190. Point2i prev_match = Point2(-1, -1);
  191. bool selection_enabled = text_editor->is_selection_active();
  192. Point2i selection_begin, selection_end;
  193. if (selection_enabled) {
  194. selection_begin = Point2i(text_editor->get_selection_from_line(), text_editor->get_selection_from_column());
  195. selection_end = Point2i(text_editor->get_selection_to_line(), text_editor->get_selection_to_column());
  196. }
  197. int vsval = text_editor->get_v_scroll();
  198. text_editor->cursor_set_line(0);
  199. text_editor->cursor_set_column(0);
  200. String replace_text = get_replace_text();
  201. int search_text_len = get_search_text().length();
  202. int rc = 0;
  203. replace_all_mode = true;
  204. text_editor->begin_complex_operation();
  205. if (selection_enabled && is_selection_only()) {
  206. text_editor->cursor_set_line(selection_begin.width);
  207. text_editor->cursor_set_column(selection_begin.height);
  208. }
  209. if (search_current()) {
  210. do {
  211. // replace area
  212. Point2i match_from(result_line, result_col);
  213. Point2i match_to(result_line, result_col + search_text_len);
  214. if (match_from < prev_match) {
  215. break; // Done.
  216. }
  217. prev_match = Point2i(result_line, result_col + replace_text.length());
  218. text_editor->unfold_line(result_line);
  219. text_editor->select(result_line, result_col, result_line, match_to.y);
  220. if (selection_enabled && is_selection_only()) {
  221. if (match_from < selection_begin || match_to > selection_end) {
  222. break; // Done.
  223. }
  224. // Replace but adjust selection bounds.
  225. text_editor->insert_text_at_cursor(replace_text);
  226. if (match_to.x == selection_end.x) {
  227. selection_end.y += replace_text.length() - search_text_len;
  228. }
  229. } else {
  230. // Just replace.
  231. text_editor->insert_text_at_cursor(replace_text);
  232. }
  233. rc++;
  234. } while (search_next());
  235. }
  236. text_editor->end_complex_operation();
  237. replace_all_mode = false;
  238. // Restore editor state (selection, cursor, scroll).
  239. text_editor->cursor_set_line(orig_cursor.x);
  240. text_editor->cursor_set_column(orig_cursor.y);
  241. if (selection_enabled && is_selection_only()) {
  242. // Reselect.
  243. text_editor->select(selection_begin.x, selection_begin.y, selection_end.x, selection_end.y);
  244. } else {
  245. text_editor->deselect();
  246. }
  247. text_editor->set_v_scroll(vsval);
  248. matches_label->add_theme_color_override("font_color", rc > 0 ? get_theme_color("font_color", "Label") : get_theme_color("error_color", "Editor"));
  249. matches_label->set_text(vformat(TTR("%d replaced."), rc));
  250. text_editor->call_deferred("connect", "text_changed", Callable(this, "_editor_text_changed"));
  251. results_count = -1;
  252. }
  253. void FindReplaceBar::_get_search_from(int &r_line, int &r_col) {
  254. r_line = text_editor->cursor_get_line();
  255. r_col = text_editor->cursor_get_column();
  256. if (text_editor->is_selection_active() && is_selection_only()) {
  257. return;
  258. }
  259. if (r_line == result_line && r_col >= result_col && r_col <= result_col + get_search_text().length()) {
  260. r_col = result_col;
  261. }
  262. }
  263. void FindReplaceBar::_update_results_count() {
  264. if (results_count != -1) {
  265. return;
  266. }
  267. results_count = 0;
  268. String searched = get_search_text();
  269. if (searched.is_empty()) {
  270. return;
  271. }
  272. String full_text = text_editor->get_text();
  273. int from_pos = 0;
  274. while (true) {
  275. int pos = is_case_sensitive() ? full_text.find(searched, from_pos) : full_text.findn(searched, from_pos);
  276. if (pos == -1) {
  277. break;
  278. }
  279. int pos_subsequent = pos + searched.length();
  280. if (is_whole_words()) {
  281. from_pos = pos + 1; // Making sure we won't hit the same match next time, if we get out via a continue.
  282. if (pos > 0 && !(is_symbol(full_text[pos - 1]) || full_text[pos - 1] == '\n')) {
  283. continue;
  284. }
  285. if (pos_subsequent < full_text.length() && !(is_symbol(full_text[pos_subsequent]) || full_text[pos_subsequent] == '\n')) {
  286. continue;
  287. }
  288. }
  289. results_count++;
  290. from_pos = pos_subsequent;
  291. }
  292. }
  293. void FindReplaceBar::_update_matches_label() {
  294. if (search_text->get_text().is_empty() || results_count == -1) {
  295. matches_label->hide();
  296. } else {
  297. matches_label->show();
  298. matches_label->add_theme_color_override("font_color", results_count > 0 ? get_theme_color("font_color", "Label") : get_theme_color("error_color", "Editor"));
  299. matches_label->set_text(vformat(results_count == 1 ? TTR("%d match.") : TTR("%d matches."), results_count));
  300. }
  301. }
  302. bool FindReplaceBar::search_current() {
  303. uint32_t flags = 0;
  304. if (is_whole_words()) {
  305. flags |= TextEdit::SEARCH_WHOLE_WORDS;
  306. }
  307. if (is_case_sensitive()) {
  308. flags |= TextEdit::SEARCH_MATCH_CASE;
  309. }
  310. int line, col;
  311. _get_search_from(line, col);
  312. return _search(flags, line, col);
  313. }
  314. bool FindReplaceBar::search_prev() {
  315. if (!is_visible()) {
  316. popup_search(true);
  317. }
  318. uint32_t flags = 0;
  319. String text = get_search_text();
  320. if (is_whole_words()) {
  321. flags |= TextEdit::SEARCH_WHOLE_WORDS;
  322. }
  323. if (is_case_sensitive()) {
  324. flags |= TextEdit::SEARCH_MATCH_CASE;
  325. }
  326. flags |= TextEdit::SEARCH_BACKWARDS;
  327. int line, col;
  328. _get_search_from(line, col);
  329. if (text_editor->is_selection_active()) {
  330. col--; // Skip currently selected word.
  331. }
  332. col -= text.length();
  333. if (col < 0) {
  334. line -= 1;
  335. if (line < 0) {
  336. line = text_editor->get_line_count() - 1;
  337. }
  338. col = text_editor->get_line(line).length();
  339. }
  340. return _search(flags, line, col);
  341. }
  342. bool FindReplaceBar::search_next() {
  343. if (!is_visible()) {
  344. popup_search(true);
  345. }
  346. uint32_t flags = 0;
  347. String text;
  348. if (replace_all_mode) {
  349. text = get_replace_text();
  350. } else {
  351. text = get_search_text();
  352. }
  353. if (is_whole_words()) {
  354. flags |= TextEdit::SEARCH_WHOLE_WORDS;
  355. }
  356. if (is_case_sensitive()) {
  357. flags |= TextEdit::SEARCH_MATCH_CASE;
  358. }
  359. int line, col;
  360. _get_search_from(line, col);
  361. if (line == result_line && col == result_col) {
  362. col += text.length();
  363. if (col > text_editor->get_line(line).length()) {
  364. line += 1;
  365. if (line >= text_editor->get_line_count()) {
  366. line = 0;
  367. }
  368. col = 0;
  369. }
  370. }
  371. return _search(flags, line, col);
  372. }
  373. void FindReplaceBar::_hide_bar() {
  374. if (replace_text->has_focus() || search_text->has_focus()) {
  375. text_editor->grab_focus();
  376. }
  377. text_editor->set_search_text("");
  378. result_line = -1;
  379. result_col = -1;
  380. hide();
  381. }
  382. void FindReplaceBar::_show_search(bool p_focus_replace, bool p_show_only) {
  383. show();
  384. if (p_show_only) {
  385. return;
  386. }
  387. if (p_focus_replace) {
  388. search_text->deselect();
  389. replace_text->call_deferred("grab_focus");
  390. } else {
  391. replace_text->deselect();
  392. search_text->call_deferred("grab_focus");
  393. }
  394. if (text_editor->is_selection_active() && !selection_only->is_pressed()) {
  395. search_text->set_text(text_editor->get_selection_text());
  396. }
  397. if (!get_search_text().is_empty()) {
  398. if (p_focus_replace) {
  399. replace_text->select_all();
  400. replace_text->set_cursor_position(replace_text->get_text().length());
  401. } else {
  402. search_text->select_all();
  403. search_text->set_cursor_position(search_text->get_text().length());
  404. }
  405. results_count = -1;
  406. _update_results_count();
  407. _update_matches_label();
  408. }
  409. }
  410. void FindReplaceBar::popup_search(bool p_show_only) {
  411. replace_text->hide();
  412. hbc_button_replace->hide();
  413. hbc_option_replace->hide();
  414. _show_search(false, p_show_only);
  415. }
  416. void FindReplaceBar::popup_replace() {
  417. if (!replace_text->is_visible_in_tree()) {
  418. replace_text->show();
  419. hbc_button_replace->show();
  420. hbc_option_replace->show();
  421. }
  422. selection_only->set_pressed((text_editor->is_selection_active() && text_editor->get_selection_from_line() < text_editor->get_selection_to_line()));
  423. _show_search(is_visible() || text_editor->is_selection_active());
  424. }
  425. void FindReplaceBar::_search_options_changed(bool p_pressed) {
  426. results_count = -1;
  427. search_current();
  428. }
  429. void FindReplaceBar::_editor_text_changed() {
  430. results_count = -1;
  431. if (is_visible_in_tree()) {
  432. preserve_cursor = true;
  433. search_current();
  434. preserve_cursor = false;
  435. }
  436. }
  437. void FindReplaceBar::_search_text_changed(const String &p_text) {
  438. results_count = -1;
  439. search_current();
  440. }
  441. void FindReplaceBar::_search_text_entered(const String &p_text) {
  442. if (Input::get_singleton()->is_key_pressed(KEY_SHIFT)) {
  443. search_prev();
  444. } else {
  445. search_next();
  446. }
  447. }
  448. void FindReplaceBar::_replace_text_entered(const String &p_text) {
  449. if (selection_only->is_pressed() && text_editor->is_selection_active()) {
  450. _replace_all();
  451. _hide_bar();
  452. } else if (Input::get_singleton()->is_key_pressed(KEY_SHIFT)) {
  453. _replace();
  454. search_prev();
  455. } else {
  456. _replace();
  457. }
  458. }
  459. String FindReplaceBar::get_search_text() const {
  460. return search_text->get_text();
  461. }
  462. String FindReplaceBar::get_replace_text() const {
  463. return replace_text->get_text();
  464. }
  465. bool FindReplaceBar::is_case_sensitive() const {
  466. return case_sensitive->is_pressed();
  467. }
  468. bool FindReplaceBar::is_whole_words() const {
  469. return whole_words->is_pressed();
  470. }
  471. bool FindReplaceBar::is_selection_only() const {
  472. return selection_only->is_pressed();
  473. }
  474. void FindReplaceBar::set_error(const String &p_label) {
  475. emit_signal("error", p_label);
  476. }
  477. void FindReplaceBar::set_text_edit(CodeEdit *p_text_edit) {
  478. results_count = -1;
  479. text_editor = p_text_edit;
  480. text_editor->connect("text_changed", callable_mp(this, &FindReplaceBar::_editor_text_changed));
  481. }
  482. void FindReplaceBar::_bind_methods() {
  483. ClassDB::bind_method("_unhandled_input", &FindReplaceBar::_unhandled_input);
  484. ClassDB::bind_method("_search_current", &FindReplaceBar::search_current);
  485. ADD_SIGNAL(MethodInfo("search"));
  486. ADD_SIGNAL(MethodInfo("error"));
  487. }
  488. FindReplaceBar::FindReplaceBar() {
  489. results_count = -1;
  490. replace_all_mode = false;
  491. preserve_cursor = false;
  492. vbc_lineedit = memnew(VBoxContainer);
  493. add_child(vbc_lineedit);
  494. vbc_lineedit->set_alignment(ALIGN_CENTER);
  495. vbc_lineedit->set_h_size_flags(SIZE_EXPAND_FILL);
  496. VBoxContainer *vbc_button = memnew(VBoxContainer);
  497. add_child(vbc_button);
  498. VBoxContainer *vbc_option = memnew(VBoxContainer);
  499. add_child(vbc_option);
  500. HBoxContainer *hbc_button_search = memnew(HBoxContainer);
  501. vbc_button->add_child(hbc_button_search);
  502. hbc_button_search->set_alignment(ALIGN_END);
  503. hbc_button_replace = memnew(HBoxContainer);
  504. vbc_button->add_child(hbc_button_replace);
  505. hbc_button_replace->set_alignment(ALIGN_END);
  506. HBoxContainer *hbc_option_search = memnew(HBoxContainer);
  507. vbc_option->add_child(hbc_option_search);
  508. hbc_option_replace = memnew(HBoxContainer);
  509. vbc_option->add_child(hbc_option_replace);
  510. // search toolbar
  511. search_text = memnew(LineEdit);
  512. vbc_lineedit->add_child(search_text);
  513. search_text->set_custom_minimum_size(Size2(100 * EDSCALE, 0));
  514. search_text->connect("text_changed", callable_mp(this, &FindReplaceBar::_search_text_changed));
  515. search_text->connect("text_entered", callable_mp(this, &FindReplaceBar::_search_text_entered));
  516. matches_label = memnew(Label);
  517. hbc_button_search->add_child(matches_label);
  518. matches_label->hide();
  519. find_prev = memnew(Button);
  520. find_prev->set_flat(true);
  521. hbc_button_search->add_child(find_prev);
  522. find_prev->set_focus_mode(FOCUS_NONE);
  523. find_prev->connect("pressed", callable_mp(this, &FindReplaceBar::search_prev));
  524. find_next = memnew(Button);
  525. find_next->set_flat(true);
  526. hbc_button_search->add_child(find_next);
  527. find_next->set_focus_mode(FOCUS_NONE);
  528. find_next->connect("pressed", callable_mp(this, &FindReplaceBar::search_next));
  529. case_sensitive = memnew(CheckBox);
  530. hbc_option_search->add_child(case_sensitive);
  531. case_sensitive->set_text(TTR("Match Case"));
  532. case_sensitive->set_focus_mode(FOCUS_NONE);
  533. case_sensitive->connect("toggled", callable_mp(this, &FindReplaceBar::_search_options_changed));
  534. whole_words = memnew(CheckBox);
  535. hbc_option_search->add_child(whole_words);
  536. whole_words->set_text(TTR("Whole Words"));
  537. whole_words->set_focus_mode(FOCUS_NONE);
  538. whole_words->connect("toggled", callable_mp(this, &FindReplaceBar::_search_options_changed));
  539. // replace toolbar
  540. replace_text = memnew(LineEdit);
  541. vbc_lineedit->add_child(replace_text);
  542. replace_text->set_custom_minimum_size(Size2(100 * EDSCALE, 0));
  543. replace_text->connect("text_entered", callable_mp(this, &FindReplaceBar::_replace_text_entered));
  544. replace = memnew(Button);
  545. hbc_button_replace->add_child(replace);
  546. replace->set_text(TTR("Replace"));
  547. replace->connect("pressed", callable_mp(this, &FindReplaceBar::_replace));
  548. replace_all = memnew(Button);
  549. hbc_button_replace->add_child(replace_all);
  550. replace_all->set_text(TTR("Replace All"));
  551. replace_all->connect("pressed", callable_mp(this, &FindReplaceBar::_replace_all));
  552. selection_only = memnew(CheckBox);
  553. hbc_option_replace->add_child(selection_only);
  554. selection_only->set_text(TTR("Selection Only"));
  555. selection_only->set_focus_mode(FOCUS_NONE);
  556. selection_only->connect("toggled", callable_mp(this, &FindReplaceBar::_search_options_changed));
  557. hide_button = memnew(TextureButton);
  558. add_child(hide_button);
  559. hide_button->set_focus_mode(FOCUS_NONE);
  560. hide_button->connect("pressed", callable_mp(this, &FindReplaceBar::_hide_bar));
  561. hide_button->set_v_size_flags(SIZE_SHRINK_CENTER);
  562. }
  563. /*** CODE EDITOR ****/
  564. // This function should be used to handle shortcuts that could otherwise
  565. // be handled too late if they weren't handled here.
  566. void CodeTextEditor::_input(const Ref<InputEvent> &event) {
  567. const Ref<InputEventKey> key_event = event;
  568. if (!key_event.is_valid() || !key_event->is_pressed() || !text_editor->has_focus()) {
  569. return;
  570. }
  571. if (ED_IS_SHORTCUT("script_text_editor/move_up", key_event)) {
  572. move_lines_up();
  573. accept_event();
  574. return;
  575. }
  576. if (ED_IS_SHORTCUT("script_text_editor/move_down", key_event)) {
  577. move_lines_down();
  578. accept_event();
  579. return;
  580. }
  581. if (ED_IS_SHORTCUT("script_text_editor/delete_line", key_event)) {
  582. delete_lines();
  583. accept_event();
  584. return;
  585. }
  586. if (ED_IS_SHORTCUT("script_text_editor/clone_down", key_event)) {
  587. clone_lines_down();
  588. accept_event();
  589. return;
  590. }
  591. }
  592. void CodeTextEditor::_text_editor_gui_input(const Ref<InputEvent> &p_event) {
  593. Ref<InputEventMouseButton> mb = p_event;
  594. if (mb.is_valid()) {
  595. if (mb->is_pressed() && mb->get_command()) {
  596. if (mb->get_button_index() == BUTTON_WHEEL_UP) {
  597. _zoom_in();
  598. } else if (mb->get_button_index() == BUTTON_WHEEL_DOWN) {
  599. _zoom_out();
  600. }
  601. }
  602. }
  603. Ref<InputEventMagnifyGesture> magnify_gesture = p_event;
  604. if (magnify_gesture.is_valid()) {
  605. font_size = text_editor->get_theme_font_size("font_size");
  606. font_size *= powf(magnify_gesture->get_factor(), 0.25);
  607. _add_font_size((int)font_size - text_editor->get_theme_font_size("font_size"));
  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. EditorSettings::get_singleton()->set("interface/editor/code_font_size", 14);
  640. text_editor->add_theme_font_size_override("font_size", 14 * EDSCALE);
  641. }
  642. void CodeTextEditor::_line_col_changed() {
  643. String line = text_editor->get_line(text_editor->cursor_get_line());
  644. int positional_column = 0;
  645. for (int i = 0; i < text_editor->cursor_get_column(); i++) {
  646. if (line[i] == '\t') {
  647. positional_column += text_editor->get_indent_size(); //tab size
  648. } else {
  649. positional_column += 1;
  650. }
  651. }
  652. StringBuilder sb;
  653. sb.append("(");
  654. sb.append(itos(text_editor->cursor_get_line() + 1).lpad(3));
  655. sb.append(",");
  656. sb.append(itos(positional_column + 1).lpad(3));
  657. sb.append(")");
  658. line_and_col_txt->set_text(sb.as_string());
  659. }
  660. void CodeTextEditor::_text_changed() {
  661. if (text_editor->is_insert_text_operation()) {
  662. code_complete_timer->start();
  663. }
  664. idle->start();
  665. }
  666. void CodeTextEditor::_code_complete_timer_timeout() {
  667. if (!is_visible_in_tree()) {
  668. return;
  669. }
  670. text_editor->query_code_comple();
  671. }
  672. void CodeTextEditor::_complete_request() {
  673. List<ScriptCodeCompletionOption> entries;
  674. String ctext = text_editor->get_text_for_completion();
  675. _code_complete_script(ctext, &entries);
  676. bool forced = false;
  677. if (code_complete_func) {
  678. code_complete_func(code_complete_ud, ctext, &entries, forced);
  679. }
  680. if (entries.size() == 0) {
  681. return;
  682. }
  683. for (List<ScriptCodeCompletionOption>::Element *E = entries.front(); E; E = E->next()) {
  684. ScriptCodeCompletionOption *e = &E->get();
  685. e->icon = _get_completion_icon(*e);
  686. e->font_color = completion_font_color;
  687. if (e->insert_text.begins_with("\"") || e->insert_text.begins_with("\'")) {
  688. e->font_color = completion_string_color;
  689. } else if (e->insert_text.begins_with("#") || e->insert_text.begins_with("//")) {
  690. e->font_color = completion_comment_color;
  691. }
  692. }
  693. text_editor->code_complete(entries, forced);
  694. }
  695. Ref<Texture2D> CodeTextEditor::_get_completion_icon(const ScriptCodeCompletionOption &p_option) {
  696. Ref<Texture2D> tex;
  697. switch (p_option.kind) {
  698. case ScriptCodeCompletionOption::KIND_CLASS: {
  699. if (has_theme_icon(p_option.display, "EditorIcons")) {
  700. tex = get_theme_icon(p_option.display, "EditorIcons");
  701. } else {
  702. tex = get_theme_icon("Object", "EditorIcons");
  703. }
  704. } break;
  705. case ScriptCodeCompletionOption::KIND_ENUM:
  706. tex = get_theme_icon("Enum", "EditorIcons");
  707. break;
  708. case ScriptCodeCompletionOption::KIND_FILE_PATH:
  709. tex = get_theme_icon("File", "EditorIcons");
  710. break;
  711. case ScriptCodeCompletionOption::KIND_NODE_PATH:
  712. tex = get_theme_icon("NodePath", "EditorIcons");
  713. break;
  714. case ScriptCodeCompletionOption::KIND_VARIABLE:
  715. tex = get_theme_icon("Variant", "EditorIcons");
  716. break;
  717. case ScriptCodeCompletionOption::KIND_CONSTANT:
  718. tex = get_theme_icon("MemberConstant", "EditorIcons");
  719. break;
  720. case ScriptCodeCompletionOption::KIND_MEMBER:
  721. tex = get_theme_icon("MemberProperty", "EditorIcons");
  722. break;
  723. case ScriptCodeCompletionOption::KIND_SIGNAL:
  724. tex = get_theme_icon("MemberSignal", "EditorIcons");
  725. break;
  726. case ScriptCodeCompletionOption::KIND_FUNCTION:
  727. tex = get_theme_icon("MemberMethod", "EditorIcons");
  728. break;
  729. case ScriptCodeCompletionOption::KIND_PLAIN_TEXT:
  730. tex = get_theme_icon("BoxMesh", "EditorIcons");
  731. break;
  732. default:
  733. tex = get_theme_icon("String", "EditorIcons");
  734. break;
  735. }
  736. return tex;
  737. }
  738. void CodeTextEditor::_font_resize_timeout() {
  739. if (_add_font_size(font_resize_val)) {
  740. font_resize_val = 0;
  741. }
  742. }
  743. bool CodeTextEditor::_add_font_size(int p_delta) {
  744. int old_size = text_editor->get_theme_font_size("font_size");
  745. int new_size = CLAMP(old_size + p_delta, 8 * EDSCALE, 96 * EDSCALE);
  746. if (new_size != old_size) {
  747. EditorSettings::get_singleton()->set("interface/editor/code_font_size", new_size / EDSCALE);
  748. text_editor->add_theme_font_size_override("font_size", new_size);
  749. }
  750. return true;
  751. }
  752. void CodeTextEditor::update_editor_settings() {
  753. completion_font_color = EDITOR_GET("text_editor/highlighting/completion_font_color");
  754. completion_string_color = EDITOR_GET("text_editor/highlighting/string_color");
  755. completion_comment_color = EDITOR_GET("text_editor/highlighting/comment_color");
  756. text_editor->set_highlight_all_occurrences(EditorSettings::get_singleton()->get("text_editor/highlighting/highlight_all_occurrences"));
  757. text_editor->set_highlight_current_line(EditorSettings::get_singleton()->get("text_editor/highlighting/highlight_current_line"));
  758. text_editor->set_indent_using_spaces(EditorSettings::get_singleton()->get("text_editor/indent/type"));
  759. text_editor->set_indent_size(EditorSettings::get_singleton()->get("text_editor/indent/size"));
  760. text_editor->set_auto_indent(EditorSettings::get_singleton()->get("text_editor/indent/auto_indent"));
  761. text_editor->set_draw_tabs(EditorSettings::get_singleton()->get("text_editor/indent/draw_tabs"));
  762. text_editor->set_draw_spaces(EditorSettings::get_singleton()->get("text_editor/indent/draw_spaces"));
  763. text_editor->set_smooth_scroll_enabled(EditorSettings::get_singleton()->get("text_editor/navigation/smooth_scrolling"));
  764. text_editor->set_v_scroll_speed(EditorSettings::get_singleton()->get("text_editor/navigation/v_scroll_speed"));
  765. text_editor->set_draw_minimap(EditorSettings::get_singleton()->get("text_editor/navigation/show_minimap"));
  766. text_editor->set_minimap_width((int)EditorSettings::get_singleton()->get("text_editor/navigation/minimap_width") * EDSCALE);
  767. text_editor->set_draw_line_numbers(EditorSettings::get_singleton()->get("text_editor/appearance/show_line_numbers"));
  768. text_editor->set_line_numbers_zero_padded(EditorSettings::get_singleton()->get("text_editor/appearance/line_numbers_zero_padded"));
  769. text_editor->set_draw_bookmarks_gutter(EditorSettings::get_singleton()->get("text_editor/appearance/show_bookmark_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. int cursor_line = text_editor->cursor_get_line();
  947. for (int i = from_line; i <= to_line; i++) {
  948. int line_id = i;
  949. int next_id = i - 1;
  950. if (line_id == 0 || next_id < 0) {
  951. return;
  952. }
  953. text_editor->unfold_line(line_id);
  954. text_editor->unfold_line(next_id);
  955. text_editor->swap_lines(line_id, next_id);
  956. text_editor->cursor_set_line(next_id);
  957. }
  958. int from_line_up = from_line > 0 ? from_line - 1 : from_line;
  959. int to_line_up = to_line > 0 ? to_line - 1 : to_line;
  960. int cursor_line_up = cursor_line > 0 ? cursor_line - 1 : cursor_line;
  961. text_editor->select(from_line_up, from_col, to_line_up, to_column);
  962. text_editor->cursor_set_line(cursor_line_up);
  963. } else {
  964. int line_id = text_editor->cursor_get_line();
  965. int next_id = line_id - 1;
  966. if (line_id == 0 || next_id < 0) {
  967. return;
  968. }
  969. text_editor->unfold_line(line_id);
  970. text_editor->unfold_line(next_id);
  971. text_editor->swap_lines(line_id, next_id);
  972. text_editor->cursor_set_line(next_id);
  973. }
  974. text_editor->end_complex_operation();
  975. text_editor->update();
  976. }
  977. void CodeTextEditor::move_lines_down() {
  978. text_editor->begin_complex_operation();
  979. if (text_editor->is_selection_active()) {
  980. int from_line = text_editor->get_selection_from_line();
  981. int from_col = text_editor->get_selection_from_column();
  982. int to_line = text_editor->get_selection_to_line();
  983. int to_column = text_editor->get_selection_to_column();
  984. int cursor_line = text_editor->cursor_get_line();
  985. for (int i = to_line; i >= from_line; i--) {
  986. int line_id = i;
  987. int next_id = i + 1;
  988. if (line_id == text_editor->get_line_count() - 1 || next_id > text_editor->get_line_count()) {
  989. return;
  990. }
  991. text_editor->unfold_line(line_id);
  992. text_editor->unfold_line(next_id);
  993. text_editor->swap_lines(line_id, next_id);
  994. text_editor->cursor_set_line(next_id);
  995. }
  996. int from_line_down = from_line < text_editor->get_line_count() ? from_line + 1 : from_line;
  997. int to_line_down = to_line < text_editor->get_line_count() ? to_line + 1 : to_line;
  998. int cursor_line_down = cursor_line < text_editor->get_line_count() ? cursor_line + 1 : cursor_line;
  999. text_editor->select(from_line_down, from_col, to_line_down, to_column);
  1000. text_editor->cursor_set_line(cursor_line_down);
  1001. } else {
  1002. int line_id = text_editor->cursor_get_line();
  1003. int next_id = line_id + 1;
  1004. if (line_id == text_editor->get_line_count() - 1 || next_id > text_editor->get_line_count()) {
  1005. return;
  1006. }
  1007. text_editor->unfold_line(line_id);
  1008. text_editor->unfold_line(next_id);
  1009. text_editor->swap_lines(line_id, next_id);
  1010. text_editor->cursor_set_line(next_id);
  1011. }
  1012. text_editor->end_complex_operation();
  1013. text_editor->update();
  1014. }
  1015. void CodeTextEditor::_delete_line(int p_line) {
  1016. // this is currently intended to be called within delete_lines()
  1017. // so `begin_complex_operation` is omitted here
  1018. text_editor->set_line(p_line, "");
  1019. if (p_line == 0 && text_editor->get_line_count() > 1) {
  1020. text_editor->cursor_set_line(1);
  1021. text_editor->cursor_set_column(0);
  1022. }
  1023. text_editor->backspace_at_cursor();
  1024. text_editor->unfold_line(p_line);
  1025. text_editor->cursor_set_line(p_line);
  1026. }
  1027. void CodeTextEditor::delete_lines() {
  1028. text_editor->begin_complex_operation();
  1029. if (text_editor->is_selection_active()) {
  1030. int to_line = text_editor->get_selection_to_line();
  1031. int from_line = text_editor->get_selection_from_line();
  1032. int count = Math::abs(to_line - from_line) + 1;
  1033. text_editor->cursor_set_line(from_line, false);
  1034. for (int i = 0; i < count; i++) {
  1035. _delete_line(from_line);
  1036. }
  1037. text_editor->deselect();
  1038. } else {
  1039. _delete_line(text_editor->cursor_get_line());
  1040. }
  1041. text_editor->end_complex_operation();
  1042. }
  1043. void CodeTextEditor::clone_lines_down() {
  1044. const int cursor_column = text_editor->cursor_get_column();
  1045. int from_line = text_editor->cursor_get_line();
  1046. int to_line = text_editor->cursor_get_line();
  1047. int from_column = 0;
  1048. int to_column = 0;
  1049. int cursor_new_line = to_line + 1;
  1050. int cursor_new_column = text_editor->cursor_get_column();
  1051. String new_text = "\n" + text_editor->get_line(from_line);
  1052. bool selection_active = false;
  1053. text_editor->cursor_set_column(text_editor->get_line(from_line).length());
  1054. if (text_editor->is_selection_active()) {
  1055. from_column = text_editor->get_selection_from_column();
  1056. to_column = text_editor->get_selection_to_column();
  1057. from_line = text_editor->get_selection_from_line();
  1058. to_line = text_editor->get_selection_to_line();
  1059. cursor_new_line = to_line + text_editor->cursor_get_line() - from_line;
  1060. cursor_new_column = to_column == cursor_column ? 2 * to_column - from_column : to_column;
  1061. new_text = text_editor->get_selection_text();
  1062. selection_active = true;
  1063. text_editor->cursor_set_line(to_line);
  1064. text_editor->cursor_set_column(to_column);
  1065. }
  1066. text_editor->begin_complex_operation();
  1067. for (int i = from_line; i <= to_line; i++) {
  1068. text_editor->unfold_line(i);
  1069. }
  1070. text_editor->deselect();
  1071. text_editor->insert_text_at_cursor(new_text);
  1072. text_editor->cursor_set_line(cursor_new_line);
  1073. text_editor->cursor_set_column(cursor_new_column);
  1074. if (selection_active) {
  1075. text_editor->select(to_line, to_column, 2 * to_line - from_line, to_line == from_line ? 2 * to_column - from_column : to_column);
  1076. }
  1077. text_editor->end_complex_operation();
  1078. text_editor->update();
  1079. }
  1080. void CodeTextEditor::toggle_inline_comment(const String &delimiter) {
  1081. text_editor->begin_complex_operation();
  1082. if (text_editor->is_selection_active()) {
  1083. int begin = text_editor->get_selection_from_line();
  1084. int end = text_editor->get_selection_to_line();
  1085. // End of selection ends on the first column of the last line, ignore it.
  1086. if (text_editor->get_selection_to_column() == 0) {
  1087. end -= 1;
  1088. }
  1089. int col_to = text_editor->get_selection_to_column();
  1090. int cursor_pos = text_editor->cursor_get_column();
  1091. // Check if all lines in the selected block are commented.
  1092. bool is_commented = true;
  1093. for (int i = begin; i <= end; i++) {
  1094. if (!text_editor->get_line(i).begins_with(delimiter)) {
  1095. is_commented = false;
  1096. break;
  1097. }
  1098. }
  1099. for (int i = begin; i <= end; i++) {
  1100. String line_text = text_editor->get_line(i);
  1101. if (line_text.strip_edges().is_empty()) {
  1102. line_text = delimiter;
  1103. } else {
  1104. if (is_commented) {
  1105. line_text = line_text.substr(delimiter.length(), line_text.length());
  1106. } else {
  1107. line_text = delimiter + line_text;
  1108. }
  1109. }
  1110. text_editor->set_line(i, line_text);
  1111. }
  1112. // Adjust selection & cursor position.
  1113. int offset = (is_commented ? -1 : 1) * delimiter.length();
  1114. int col_from = text_editor->get_selection_from_column() > 0 ? text_editor->get_selection_from_column() + offset : 0;
  1115. if (is_commented && text_editor->cursor_get_column() == text_editor->get_line(text_editor->cursor_get_line()).length() + 1) {
  1116. cursor_pos += 1;
  1117. }
  1118. if (text_editor->get_selection_to_column() != 0 && col_to != text_editor->get_line(text_editor->get_selection_to_line()).length() + 1) {
  1119. col_to += offset;
  1120. }
  1121. if (text_editor->cursor_get_column() != 0) {
  1122. cursor_pos += offset;
  1123. }
  1124. text_editor->select(begin, col_from, text_editor->get_selection_to_line(), col_to);
  1125. text_editor->cursor_set_column(cursor_pos);
  1126. } else {
  1127. int begin = text_editor->cursor_get_line();
  1128. String line_text = text_editor->get_line(begin);
  1129. int delimiter_length = delimiter.length();
  1130. int col = text_editor->cursor_get_column();
  1131. if (line_text.begins_with(delimiter)) {
  1132. line_text = line_text.substr(delimiter_length, line_text.length());
  1133. col -= delimiter_length;
  1134. } else {
  1135. line_text = delimiter + line_text;
  1136. col += delimiter_length;
  1137. }
  1138. text_editor->set_line(begin, line_text);
  1139. text_editor->cursor_set_column(col);
  1140. }
  1141. text_editor->end_complex_operation();
  1142. text_editor->update();
  1143. }
  1144. void CodeTextEditor::goto_line(int p_line) {
  1145. text_editor->deselect();
  1146. text_editor->unfold_line(p_line);
  1147. text_editor->call_deferred("cursor_set_line", p_line);
  1148. }
  1149. void CodeTextEditor::goto_line_selection(int p_line, int p_begin, int p_end) {
  1150. text_editor->unfold_line(p_line);
  1151. text_editor->call_deferred("cursor_set_line", p_line);
  1152. text_editor->call_deferred("cursor_set_column", p_begin);
  1153. text_editor->select(p_line, p_begin, p_line, p_end);
  1154. }
  1155. void CodeTextEditor::goto_line_centered(int p_line) {
  1156. goto_line(p_line);
  1157. text_editor->call_deferred("center_viewport_to_cursor");
  1158. }
  1159. void CodeTextEditor::set_executing_line(int p_line) {
  1160. text_editor->set_line_as_executing(p_line, true);
  1161. }
  1162. void CodeTextEditor::clear_executing_line() {
  1163. text_editor->clear_executing_lines();
  1164. }
  1165. Variant CodeTextEditor::get_edit_state() {
  1166. Dictionary state;
  1167. state["scroll_position"] = text_editor->get_v_scroll();
  1168. state["h_scroll_position"] = text_editor->get_h_scroll();
  1169. state["column"] = text_editor->cursor_get_column();
  1170. state["row"] = text_editor->cursor_get_line();
  1171. state["selection"] = get_text_editor()->is_selection_active();
  1172. if (get_text_editor()->is_selection_active()) {
  1173. state["selection_from_line"] = text_editor->get_selection_from_line();
  1174. state["selection_from_column"] = text_editor->get_selection_from_column();
  1175. state["selection_to_line"] = text_editor->get_selection_to_line();
  1176. state["selection_to_column"] = text_editor->get_selection_to_column();
  1177. }
  1178. state["folded_lines"] = text_editor->get_folded_lines();
  1179. state["breakpoints"] = text_editor->get_breakpointed_lines();
  1180. state["bookmarks"] = text_editor->get_bookmarked_lines();
  1181. Ref<EditorSyntaxHighlighter> syntax_highlighter = text_editor->get_syntax_highlighter();
  1182. state["syntax_highlighter"] = syntax_highlighter->_get_name();
  1183. return state;
  1184. }
  1185. void CodeTextEditor::set_edit_state(const Variant &p_state) {
  1186. Dictionary state = p_state;
  1187. /* update the row first as it sets the column to 0 */
  1188. text_editor->cursor_set_line(state["row"]);
  1189. text_editor->cursor_set_column(state["column"]);
  1190. text_editor->set_v_scroll(state["scroll_position"]);
  1191. text_editor->set_h_scroll(state["h_scroll_position"]);
  1192. if (state.has("selection")) {
  1193. text_editor->select(state["selection_from_line"], state["selection_from_column"], state["selection_to_line"], state["selection_to_column"]);
  1194. }
  1195. if (state.has("folded_lines")) {
  1196. Vector<int> folded_lines = state["folded_lines"];
  1197. for (int i = 0; i < folded_lines.size(); i++) {
  1198. text_editor->fold_line(folded_lines[i]);
  1199. }
  1200. }
  1201. if (state.has("breakpoints")) {
  1202. Array breakpoints = state["breakpoints"];
  1203. for (int i = 0; i < breakpoints.size(); i++) {
  1204. text_editor->set_line_as_breakpoint(breakpoints[i], true);
  1205. }
  1206. }
  1207. if (state.has("bookmarks")) {
  1208. Array bookmarks = state["bookmarks"];
  1209. for (int i = 0; i < bookmarks.size(); i++) {
  1210. text_editor->set_line_as_bookmarked(bookmarks[i], true);
  1211. }
  1212. }
  1213. }
  1214. void CodeTextEditor::set_error(const String &p_error) {
  1215. error->set_text(p_error);
  1216. if (p_error != "") {
  1217. error->set_default_cursor_shape(CURSOR_POINTING_HAND);
  1218. } else {
  1219. error->set_default_cursor_shape(CURSOR_ARROW);
  1220. }
  1221. }
  1222. void CodeTextEditor::set_error_pos(int p_line, int p_column) {
  1223. error_line = p_line;
  1224. error_column = p_column;
  1225. }
  1226. void CodeTextEditor::goto_error() {
  1227. if (error->get_text() != "") {
  1228. text_editor->cursor_set_line(error_line);
  1229. text_editor->cursor_set_column(error_column);
  1230. text_editor->center_viewport_to_cursor();
  1231. }
  1232. }
  1233. void CodeTextEditor::_update_font() {
  1234. text_editor->add_theme_font_override("font", get_theme_font("source", "EditorFonts"));
  1235. text_editor->add_theme_font_size_override("font_size", get_theme_font_size("source_size", "EditorFonts"));
  1236. error->add_theme_font_override("font", get_theme_font("status_source", "EditorFonts"));
  1237. error->add_theme_font_size_override("font_size", get_theme_font_size("status_source_size", "EditorFonts"));
  1238. error->add_theme_color_override("font_color", get_theme_color("error_color", "Editor"));
  1239. Ref<Font> status_bar_font = get_theme_font("status_source", "EditorFonts");
  1240. int status_bar_font_size = get_theme_font_size("status_source_size", "EditorFonts");
  1241. error->add_theme_font_override("font", status_bar_font);
  1242. error->add_theme_font_size_override("font_size", status_bar_font_size);
  1243. int count = status_bar->get_child_count();
  1244. for (int i = 0; i < count; i++) {
  1245. Control *n = Object::cast_to<Control>(status_bar->get_child(i));
  1246. if (n) {
  1247. n->add_theme_font_override("font", status_bar_font);
  1248. n->add_theme_font_size_override("font_size", status_bar_font_size);
  1249. }
  1250. }
  1251. }
  1252. void CodeTextEditor::_on_settings_change() {
  1253. _update_font();
  1254. font_size = EditorSettings::get_singleton()->get("interface/editor/code_font_size");
  1255. int ot_mode = EditorSettings::get_singleton()->get("interface/editor/code_font_contextual_ligatures");
  1256. switch (ot_mode) {
  1257. case 1: { // Disable ligatures.
  1258. text_editor->clear_opentype_features();
  1259. text_editor->set_opentype_feature("calt", 0);
  1260. } break;
  1261. case 2: { // Custom.
  1262. text_editor->clear_opentype_features();
  1263. Vector<String> subtag = String(EditorSettings::get_singleton()->get("interface/editor/code_font_custom_opentype_features")).split(",");
  1264. Dictionary ftrs;
  1265. for (int i = 0; i < subtag.size(); i++) {
  1266. Vector<String> subtag_a = subtag[i].split("=");
  1267. if (subtag_a.size() == 2) {
  1268. text_editor->set_opentype_feature(subtag_a[0], subtag_a[1].to_int());
  1269. } else if (subtag_a.size() == 1) {
  1270. text_editor->set_opentype_feature(subtag_a[0], 1);
  1271. }
  1272. }
  1273. } break;
  1274. default: { // Default.
  1275. text_editor->clear_opentype_features();
  1276. text_editor->set_opentype_feature("calt", 1);
  1277. } break;
  1278. }
  1279. // Auto brace completion.
  1280. text_editor->set_auto_brace_completion(
  1281. EDITOR_GET("text_editor/completion/auto_brace_complete"));
  1282. code_complete_timer->set_wait_time(
  1283. EDITOR_GET("text_editor/completion/code_complete_delay"));
  1284. // Call hint settings.
  1285. text_editor->set_callhint_settings(
  1286. EDITOR_GET("text_editor/completion/put_callhint_tooltip_below_current_line"),
  1287. EDITOR_GET("text_editor/completion/callhint_tooltip_offset"));
  1288. idle->set_wait_time(EDITOR_GET("text_editor/completion/idle_parse_delay"));
  1289. }
  1290. void CodeTextEditor::_text_changed_idle_timeout() {
  1291. _validate_script();
  1292. emit_signal("validate_script");
  1293. }
  1294. void CodeTextEditor::validate_script() {
  1295. idle->start();
  1296. }
  1297. void CodeTextEditor::_warning_label_gui_input(const Ref<InputEvent> &p_event) {
  1298. Ref<InputEventMouseButton> mb = p_event;
  1299. if (mb.is_valid() && mb->is_pressed() && mb->get_button_index() == BUTTON_LEFT) {
  1300. _warning_button_pressed();
  1301. }
  1302. }
  1303. void CodeTextEditor::_warning_button_pressed() {
  1304. _set_show_warnings_panel(!is_warnings_panel_opened);
  1305. }
  1306. void CodeTextEditor::_set_show_warnings_panel(bool p_show) {
  1307. is_warnings_panel_opened = p_show;
  1308. emit_signal("show_warnings_panel", p_show);
  1309. }
  1310. void CodeTextEditor::_toggle_scripts_pressed() {
  1311. if (is_layout_rtl()) {
  1312. toggle_scripts_button->set_icon(ScriptEditor::get_singleton()->toggle_scripts_panel() ? get_theme_icon("Forward", "EditorIcons") : get_theme_icon("Back", "EditorIcons"));
  1313. } else {
  1314. toggle_scripts_button->set_icon(ScriptEditor::get_singleton()->toggle_scripts_panel() ? get_theme_icon("Back", "EditorIcons") : get_theme_icon("Forward", "EditorIcons"));
  1315. }
  1316. }
  1317. void CodeTextEditor::_error_pressed(const Ref<InputEvent> &p_event) {
  1318. Ref<InputEventMouseButton> mb = p_event;
  1319. if (mb.is_valid() && mb->is_pressed() && mb->get_button_index() == BUTTON_LEFT) {
  1320. goto_error();
  1321. }
  1322. }
  1323. void CodeTextEditor::_notification(int p_what) {
  1324. switch (p_what) {
  1325. case EditorSettings::NOTIFICATION_EDITOR_SETTINGS_CHANGED: {
  1326. _load_theme_settings();
  1327. emit_signal("load_theme_settings");
  1328. } break;
  1329. case NOTIFICATION_THEME_CHANGED: {
  1330. if (toggle_scripts_button->is_visible()) {
  1331. update_toggle_scripts_button();
  1332. }
  1333. _update_font();
  1334. } break;
  1335. case NOTIFICATION_ENTER_TREE: {
  1336. warning_button->set_icon(get_theme_icon("NodeWarning", "EditorIcons"));
  1337. add_theme_constant_override("separation", 4 * EDSCALE);
  1338. } break;
  1339. case NOTIFICATION_VISIBILITY_CHANGED: {
  1340. if (toggle_scripts_button->is_visible()) {
  1341. update_toggle_scripts_button();
  1342. }
  1343. set_process_input(is_visible_in_tree());
  1344. } break;
  1345. default:
  1346. break;
  1347. }
  1348. }
  1349. void CodeTextEditor::set_warning_nb(int p_warning_nb) {
  1350. warning_count_label->set_text(itos(p_warning_nb));
  1351. warning_count_label->set_visible(p_warning_nb > 0);
  1352. warning_button->set_visible(p_warning_nb > 0);
  1353. if (!p_warning_nb) {
  1354. _set_show_warnings_panel(false);
  1355. }
  1356. }
  1357. void CodeTextEditor::toggle_bookmark() {
  1358. int line = text_editor->cursor_get_line();
  1359. text_editor->set_line_as_bookmarked(line, !text_editor->is_line_bookmarked(line));
  1360. }
  1361. void CodeTextEditor::goto_next_bookmark() {
  1362. Array bmarks = text_editor->get_bookmarked_lines();
  1363. if (bmarks.size() <= 0) {
  1364. return;
  1365. }
  1366. int line = text_editor->cursor_get_line();
  1367. if (line >= (int)bmarks[bmarks.size() - 1]) {
  1368. text_editor->unfold_line(bmarks[0]);
  1369. text_editor->cursor_set_line(bmarks[0]);
  1370. text_editor->center_viewport_to_cursor();
  1371. } else {
  1372. for (int i = 0; i < bmarks.size(); i++) {
  1373. int bmark_line = bmarks[i];
  1374. if (bmark_line > line) {
  1375. text_editor->unfold_line(bmark_line);
  1376. text_editor->cursor_set_line(bmark_line);
  1377. text_editor->center_viewport_to_cursor();
  1378. return;
  1379. }
  1380. }
  1381. }
  1382. }
  1383. void CodeTextEditor::goto_prev_bookmark() {
  1384. Array bmarks = text_editor->get_bookmarked_lines();
  1385. if (bmarks.size() <= 0) {
  1386. return;
  1387. }
  1388. int line = text_editor->cursor_get_line();
  1389. if (line <= (int)bmarks[0]) {
  1390. text_editor->unfold_line(bmarks[bmarks.size() - 1]);
  1391. text_editor->cursor_set_line(bmarks[bmarks.size() - 1]);
  1392. text_editor->center_viewport_to_cursor();
  1393. } else {
  1394. for (int i = bmarks.size(); i >= 0; i--) {
  1395. int bmark_line = bmarks[i];
  1396. if (bmark_line < line) {
  1397. text_editor->unfold_line(bmark_line);
  1398. text_editor->cursor_set_line(bmark_line);
  1399. text_editor->center_viewport_to_cursor();
  1400. return;
  1401. }
  1402. }
  1403. }
  1404. }
  1405. void CodeTextEditor::remove_all_bookmarks() {
  1406. text_editor->clear_bookmarked_lines();
  1407. }
  1408. void CodeTextEditor::_bind_methods() {
  1409. ClassDB::bind_method(D_METHOD("_input"), &CodeTextEditor::_input);
  1410. ADD_SIGNAL(MethodInfo("validate_script"));
  1411. ADD_SIGNAL(MethodInfo("load_theme_settings"));
  1412. ADD_SIGNAL(MethodInfo("show_warnings_panel"));
  1413. }
  1414. void CodeTextEditor::set_code_complete_func(CodeTextEditorCodeCompleteFunc p_code_complete_func, void *p_ud) {
  1415. code_complete_func = p_code_complete_func;
  1416. code_complete_ud = p_ud;
  1417. }
  1418. void CodeTextEditor::show_toggle_scripts_button() {
  1419. toggle_scripts_button->show();
  1420. }
  1421. void CodeTextEditor::update_toggle_scripts_button() {
  1422. if (is_layout_rtl()) {
  1423. toggle_scripts_button->set_icon(ScriptEditor::get_singleton()->is_scripts_panel_toggled() ? get_theme_icon("Forward", "EditorIcons") : get_theme_icon("Back", "EditorIcons"));
  1424. } else {
  1425. toggle_scripts_button->set_icon(ScriptEditor::get_singleton()->is_scripts_panel_toggled() ? get_theme_icon("Back", "EditorIcons") : get_theme_icon("Forward", "EditorIcons"));
  1426. }
  1427. toggle_scripts_button->set_tooltip(TTR("Toggle Scripts Panel") + " (" + ED_GET_SHORTCUT("script_editor/toggle_scripts_panel")->get_as_text() + ")");
  1428. }
  1429. CodeTextEditor::CodeTextEditor() {
  1430. code_complete_func = nullptr;
  1431. ED_SHORTCUT("script_editor/zoom_in", TTR("Zoom In"), KEY_MASK_CMD | KEY_EQUAL);
  1432. ED_SHORTCUT("script_editor/zoom_out", TTR("Zoom Out"), KEY_MASK_CMD | KEY_MINUS);
  1433. ED_SHORTCUT("script_editor/reset_zoom", TTR("Reset Zoom"), KEY_MASK_CMD | KEY_0);
  1434. text_editor = memnew(CodeEdit);
  1435. add_child(text_editor);
  1436. text_editor->set_v_size_flags(SIZE_EXPAND_FILL);
  1437. int ot_mode = EditorSettings::get_singleton()->get("interface/editor/code_font_contextual_ligatures");
  1438. switch (ot_mode) {
  1439. case 1: { // Disable ligatures.
  1440. text_editor->clear_opentype_features();
  1441. text_editor->set_opentype_feature("calt", 0);
  1442. } break;
  1443. case 2: { // Custom.
  1444. text_editor->clear_opentype_features();
  1445. Vector<String> subtag = String(EditorSettings::get_singleton()->get("interface/editor/code_font_custom_opentype_features")).split(",");
  1446. Dictionary ftrs;
  1447. for (int i = 0; i < subtag.size(); i++) {
  1448. Vector<String> subtag_a = subtag[i].split("=");
  1449. if (subtag_a.size() == 2) {
  1450. text_editor->set_opentype_feature(subtag_a[0], subtag_a[1].to_int());
  1451. } else if (subtag_a.size() == 1) {
  1452. text_editor->set_opentype_feature(subtag_a[0], 1);
  1453. }
  1454. }
  1455. } break;
  1456. default: { // Default.
  1457. text_editor->clear_opentype_features();
  1458. text_editor->set_opentype_feature("calt", 1);
  1459. } break;
  1460. }
  1461. // Added second so it opens at the bottom, so it won't shift the entire text editor when opening.
  1462. find_replace_bar = memnew(FindReplaceBar);
  1463. add_child(find_replace_bar);
  1464. find_replace_bar->set_h_size_flags(SIZE_EXPAND_FILL);
  1465. find_replace_bar->hide();
  1466. find_replace_bar->set_text_edit(text_editor);
  1467. text_editor->set_draw_line_numbers(true);
  1468. text_editor->set_brace_matching(true);
  1469. text_editor->set_auto_indent(true);
  1470. status_bar = memnew(HBoxContainer);
  1471. add_child(status_bar);
  1472. status_bar->set_h_size_flags(SIZE_EXPAND_FILL);
  1473. status_bar->set_custom_minimum_size(Size2(0, 24 * EDSCALE)); // Adjust for the height of the warning icon.
  1474. idle = memnew(Timer);
  1475. add_child(idle);
  1476. idle->set_one_shot(true);
  1477. idle->set_wait_time(EDITOR_GET("text_editor/completion/idle_parse_delay"));
  1478. code_complete_timer = memnew(Timer);
  1479. add_child(code_complete_timer);
  1480. code_complete_timer->set_one_shot(true);
  1481. code_complete_timer->set_wait_time(EDITOR_GET("text_editor/completion/code_complete_delay"));
  1482. error_line = 0;
  1483. error_column = 0;
  1484. toggle_scripts_button = memnew(Button);
  1485. toggle_scripts_button->set_flat(true);
  1486. toggle_scripts_button->connect("pressed", callable_mp(this, &CodeTextEditor::_toggle_scripts_pressed));
  1487. status_bar->add_child(toggle_scripts_button);
  1488. toggle_scripts_button->hide();
  1489. // Error
  1490. ScrollContainer *scroll = memnew(ScrollContainer);
  1491. scroll->set_h_size_flags(SIZE_EXPAND_FILL);
  1492. scroll->set_v_size_flags(SIZE_EXPAND_FILL);
  1493. scroll->set_enable_v_scroll(false);
  1494. status_bar->add_child(scroll);
  1495. error = memnew(Label);
  1496. scroll->add_child(error);
  1497. error->set_v_size_flags(SIZE_EXPAND | SIZE_SHRINK_CENTER);
  1498. error->set_mouse_filter(MOUSE_FILTER_STOP);
  1499. error->connect("gui_input", callable_mp(this, &CodeTextEditor::_error_pressed));
  1500. find_replace_bar->connect("error", callable_mp(error, &Label::set_text));
  1501. // Warnings
  1502. warning_button = memnew(Button);
  1503. warning_button->set_flat(true);
  1504. status_bar->add_child(warning_button);
  1505. warning_button->set_v_size_flags(SIZE_EXPAND | SIZE_SHRINK_CENTER);
  1506. warning_button->set_default_cursor_shape(CURSOR_POINTING_HAND);
  1507. warning_button->connect("pressed", callable_mp(this, &CodeTextEditor::_warning_button_pressed));
  1508. warning_button->set_tooltip(TTR("Warnings"));
  1509. warning_count_label = memnew(Label);
  1510. status_bar->add_child(warning_count_label);
  1511. warning_count_label->set_v_size_flags(SIZE_EXPAND | SIZE_SHRINK_CENTER);
  1512. warning_count_label->set_align(Label::ALIGN_RIGHT);
  1513. warning_count_label->set_default_cursor_shape(CURSOR_POINTING_HAND);
  1514. warning_count_label->set_mouse_filter(MOUSE_FILTER_STOP);
  1515. warning_count_label->set_tooltip(TTR("Warnings"));
  1516. warning_count_label->add_theme_color_override("font_color", EditorNode::get_singleton()->get_gui_base()->get_theme_color("warning_color", "Editor"));
  1517. warning_count_label->add_theme_font_override("font", EditorNode::get_singleton()->get_gui_base()->get_theme_font("status_source", "EditorFonts"));
  1518. warning_count_label->add_theme_font_size_override("font_size", EditorNode::get_singleton()->get_gui_base()->get_theme_font_size("status_source_size", "EditorFonts"));
  1519. warning_count_label->connect("gui_input", callable_mp(this, &CodeTextEditor::_warning_label_gui_input));
  1520. is_warnings_panel_opened = false;
  1521. set_warning_nb(0);
  1522. // Line and column
  1523. line_and_col_txt = memnew(Label);
  1524. status_bar->add_child(line_and_col_txt);
  1525. line_and_col_txt->set_v_size_flags(SIZE_EXPAND | SIZE_SHRINK_CENTER);
  1526. line_and_col_txt->add_theme_font_override("font", EditorNode::get_singleton()->get_gui_base()->get_theme_font("status_source", "EditorFonts"));
  1527. line_and_col_txt->add_theme_font_size_override("font_size", EditorNode::get_singleton()->get_gui_base()->get_theme_font_size("status_source_size", "EditorFonts"));
  1528. line_and_col_txt->set_tooltip(TTR("Line and column numbers."));
  1529. line_and_col_txt->set_mouse_filter(MOUSE_FILTER_STOP);
  1530. text_editor->connect("gui_input", callable_mp(this, &CodeTextEditor::_text_editor_gui_input));
  1531. text_editor->connect("cursor_changed", callable_mp(this, &CodeTextEditor::_line_col_changed));
  1532. text_editor->connect("text_changed", callable_mp(this, &CodeTextEditor::_text_changed));
  1533. text_editor->connect("request_completion", callable_mp(this, &CodeTextEditor::_complete_request));
  1534. Vector<String> cs;
  1535. cs.push_back(".");
  1536. cs.push_back(",");
  1537. cs.push_back("(");
  1538. cs.push_back("=");
  1539. cs.push_back("$");
  1540. cs.push_back("@");
  1541. text_editor->set_completion(true, cs);
  1542. idle->connect("timeout", callable_mp(this, &CodeTextEditor::_text_changed_idle_timeout));
  1543. code_complete_timer->connect("timeout", callable_mp(this, &CodeTextEditor::_code_complete_timer_timeout));
  1544. font_resize_val = 0;
  1545. font_size = EditorSettings::get_singleton()->get("interface/editor/code_font_size");
  1546. font_resize_timer = memnew(Timer);
  1547. add_child(font_resize_timer);
  1548. font_resize_timer->set_one_shot(true);
  1549. font_resize_timer->set_wait_time(0.07);
  1550. font_resize_timer->connect("timeout", callable_mp(this, &CodeTextEditor::_font_resize_timeout));
  1551. EditorSettings::get_singleton()->connect("settings_changed", callable_mp(this, &CodeTextEditor::_on_settings_change));
  1552. }