code_editor.cpp 59 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812
  1. /**************************************************************************/
  2. /* code_editor.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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_node.h"
  35. #include "editor/editor_settings.h"
  36. #include "editor/editor_string_names.h"
  37. #include "editor/plugins/script_editor_plugin.h"
  38. #include "editor/themes/editor_scale.h"
  39. #include "editor/themes/editor_theme_manager.h"
  40. #include "scene/gui/menu_button.h"
  41. #include "scene/gui/separator.h"
  42. #include "scene/resources/font.h"
  43. void GotoLineDialog::popup_find_line(CodeEdit *p_edit) {
  44. text_editor = p_edit;
  45. // Add 1 because text_editor->get_caret_line() starts from 0, but the editor user interface starts from 1.
  46. line->set_text(itos(text_editor->get_caret_line() + 1));
  47. line->select_all();
  48. popup_centered(Size2(180, 80) * EDSCALE);
  49. line->grab_focus();
  50. }
  51. int GotoLineDialog::get_line() const {
  52. return line->get_text().to_int();
  53. }
  54. void GotoLineDialog::ok_pressed() {
  55. // Subtract 1 because the editor user interface starts from 1, but text_editor->set_caret_line(n) starts from 0.
  56. const int line_number = get_line() - 1;
  57. if (line_number < 0 || line_number >= text_editor->get_line_count()) {
  58. return;
  59. }
  60. text_editor->remove_secondary_carets();
  61. text_editor->unfold_line(line_number);
  62. text_editor->set_caret_line(line_number);
  63. hide();
  64. }
  65. GotoLineDialog::GotoLineDialog() {
  66. set_title(TTR("Go to Line"));
  67. VBoxContainer *vbc = memnew(VBoxContainer);
  68. vbc->set_anchor_and_offset(SIDE_LEFT, Control::ANCHOR_BEGIN, 8 * EDSCALE);
  69. vbc->set_anchor_and_offset(SIDE_TOP, Control::ANCHOR_BEGIN, 8 * EDSCALE);
  70. vbc->set_anchor_and_offset(SIDE_RIGHT, Control::ANCHOR_END, -8 * EDSCALE);
  71. vbc->set_anchor_and_offset(SIDE_BOTTOM, Control::ANCHOR_END, -8 * EDSCALE);
  72. add_child(vbc);
  73. Label *l = memnew(Label);
  74. l->set_text(TTR("Line Number:"));
  75. vbc->add_child(l);
  76. line = memnew(LineEdit);
  77. vbc->add_child(line);
  78. register_text_enter(line);
  79. text_editor = nullptr;
  80. line_label = nullptr;
  81. set_hide_on_ok(false);
  82. }
  83. void FindReplaceBar::_notification(int p_what) {
  84. switch (p_what) {
  85. case EditorSettings::NOTIFICATION_EDITOR_SETTINGS_CHANGED: {
  86. if (!EditorThemeManager::is_generated_theme_outdated()) {
  87. break;
  88. }
  89. [[fallthrough]];
  90. }
  91. case NOTIFICATION_READY: {
  92. find_prev->set_icon(get_editor_theme_icon(SNAME("MoveUp")));
  93. find_next->set_icon(get_editor_theme_icon(SNAME("MoveDown")));
  94. hide_button->set_texture_normal(get_editor_theme_icon(SNAME("Close")));
  95. hide_button->set_texture_hover(get_editor_theme_icon(SNAME("Close")));
  96. hide_button->set_texture_pressed(get_editor_theme_icon(SNAME("Close")));
  97. hide_button->set_custom_minimum_size(hide_button->get_texture_normal()->get_size());
  98. } break;
  99. case NOTIFICATION_VISIBILITY_CHANGED: {
  100. set_process_unhandled_input(is_visible_in_tree());
  101. } break;
  102. case NOTIFICATION_THEME_CHANGED: {
  103. matches_label->add_theme_color_override("font_color", results_count > 0 ? get_theme_color(SNAME("font_color"), SNAME("Label")) : get_theme_color(SNAME("error_color"), EditorStringName(Editor)));
  104. } break;
  105. case NOTIFICATION_PREDELETE: {
  106. if (base_text_editor) {
  107. base_text_editor->remove_find_replace_bar();
  108. base_text_editor = nullptr;
  109. }
  110. } break;
  111. }
  112. }
  113. void FindReplaceBar::unhandled_input(const Ref<InputEvent> &p_event) {
  114. ERR_FAIL_COND(p_event.is_null());
  115. Ref<InputEventKey> k = p_event;
  116. if (k.is_valid() && k->is_action_pressed(SNAME("ui_cancel"), false, true)) {
  117. Control *focus_owner = get_viewport()->gui_get_focus_owner();
  118. if (text_editor->has_focus() || (focus_owner && is_ancestor_of(focus_owner))) {
  119. _hide_bar();
  120. accept_event();
  121. }
  122. }
  123. }
  124. void FindReplaceBar::_focus_lost() {
  125. if (Input::get_singleton()->is_action_pressed(SNAME("ui_cancel"))) {
  126. // Unfocused after pressing Escape, so hide the bar.
  127. _hide_bar(true);
  128. }
  129. }
  130. void FindReplaceBar::_update_flags(bool p_direction_backwards) {
  131. flags = 0;
  132. if (is_whole_words()) {
  133. flags |= TextEdit::SEARCH_WHOLE_WORDS;
  134. }
  135. if (is_case_sensitive()) {
  136. flags |= TextEdit::SEARCH_MATCH_CASE;
  137. }
  138. if (p_direction_backwards) {
  139. flags |= TextEdit::SEARCH_BACKWARDS;
  140. }
  141. }
  142. bool FindReplaceBar::_search(uint32_t p_flags, int p_from_line, int p_from_col) {
  143. if (!preserve_cursor) {
  144. text_editor->remove_secondary_carets();
  145. }
  146. String text = get_search_text();
  147. Point2i pos = text_editor->search(text, p_flags, p_from_line, p_from_col);
  148. if (pos.x != -1) {
  149. if (!preserve_cursor && !is_selection_only()) {
  150. text_editor->unfold_line(pos.y);
  151. text_editor->select(pos.y, pos.x, pos.y, pos.x + text.length());
  152. text_editor->center_viewport_to_caret(0);
  153. line_col_changed_for_result = true;
  154. }
  155. text_editor->set_search_text(text);
  156. text_editor->set_search_flags(p_flags);
  157. result_line = pos.y;
  158. result_col = pos.x;
  159. _update_results_count();
  160. } else {
  161. results_count = 0;
  162. result_line = -1;
  163. result_col = -1;
  164. text_editor->set_search_text("");
  165. text_editor->set_search_flags(p_flags);
  166. }
  167. _update_matches_display();
  168. return pos.x != -1;
  169. }
  170. void FindReplaceBar::_replace() {
  171. text_editor->begin_complex_operation();
  172. text_editor->remove_secondary_carets();
  173. bool selection_enabled = text_editor->has_selection(0);
  174. Point2i selection_begin, selection_end;
  175. if (selection_enabled) {
  176. selection_begin = Point2i(text_editor->get_selection_from_line(0), text_editor->get_selection_from_column(0));
  177. selection_end = Point2i(text_editor->get_selection_to_line(0), text_editor->get_selection_to_column(0));
  178. }
  179. String repl_text = get_replace_text();
  180. int search_text_len = get_search_text().length();
  181. if (selection_enabled && is_selection_only()) {
  182. // Restrict search_current() to selected region.
  183. text_editor->set_caret_line(selection_begin.width, false, true, -1, 0);
  184. text_editor->set_caret_column(selection_begin.height, true, 0);
  185. }
  186. if (search_current()) {
  187. text_editor->unfold_line(result_line);
  188. text_editor->select(result_line, result_col, result_line, result_col + search_text_len, 0);
  189. if (selection_enabled && is_selection_only()) {
  190. Point2i match_from(result_line, result_col);
  191. Point2i match_to(result_line, result_col + search_text_len);
  192. if (!(match_from < selection_begin || match_to > selection_end)) {
  193. text_editor->insert_text_at_caret(repl_text, 0);
  194. if (match_to.x == selection_end.x) {
  195. // Adjust selection bounds if necessary.
  196. selection_end.y += repl_text.length() - search_text_len;
  197. }
  198. }
  199. } else {
  200. text_editor->insert_text_at_caret(repl_text, 0);
  201. }
  202. }
  203. text_editor->end_complex_operation();
  204. results_count = -1;
  205. results_count_to_current = -1;
  206. needs_to_count_results = true;
  207. if (selection_enabled && is_selection_only()) {
  208. // Reselect in order to keep 'Replace' restricted to selection.
  209. text_editor->select(selection_begin.x, selection_begin.y, selection_end.x, selection_end.y, 0);
  210. } else {
  211. text_editor->deselect(0);
  212. }
  213. }
  214. void FindReplaceBar::_replace_all() {
  215. text_editor->begin_complex_operation();
  216. text_editor->remove_secondary_carets();
  217. text_editor->disconnect("text_changed", callable_mp(this, &FindReplaceBar::_editor_text_changed));
  218. // Line as x so it gets priority in comparison, column as y.
  219. Point2i orig_cursor(text_editor->get_caret_line(0), text_editor->get_caret_column(0));
  220. Point2i prev_match = Point2(-1, -1);
  221. bool selection_enabled = text_editor->has_selection(0);
  222. if (!is_selection_only()) {
  223. text_editor->deselect();
  224. selection_enabled = false;
  225. } else {
  226. result_line = -1;
  227. result_col = -1;
  228. }
  229. Point2i selection_begin, selection_end;
  230. if (selection_enabled) {
  231. selection_begin = Point2i(text_editor->get_selection_from_line(0), text_editor->get_selection_from_column(0));
  232. selection_end = Point2i(text_editor->get_selection_to_line(0), text_editor->get_selection_to_column(0));
  233. }
  234. int vsval = text_editor->get_v_scroll();
  235. String repl_text = get_replace_text();
  236. int search_text_len = get_search_text().length();
  237. int rc = 0;
  238. replace_all_mode = true;
  239. if (selection_enabled && is_selection_only()) {
  240. text_editor->set_caret_line(selection_begin.width, false, true, -1, 0);
  241. text_editor->set_caret_column(selection_begin.height, true, 0);
  242. } else {
  243. text_editor->set_caret_line(0, false, true, -1, 0);
  244. text_editor->set_caret_column(0, true, 0);
  245. }
  246. if (search_current()) {
  247. do {
  248. // Replace area.
  249. Point2i match_from(result_line, result_col);
  250. Point2i match_to(result_line, result_col + search_text_len);
  251. if (match_from < prev_match) {
  252. break; // Done.
  253. }
  254. prev_match = Point2i(result_line, result_col + repl_text.length());
  255. text_editor->unfold_line(result_line);
  256. text_editor->select(result_line, result_col, result_line, match_to.y, 0);
  257. if (selection_enabled) {
  258. if (match_from < selection_begin || match_to > selection_end) {
  259. break; // Done.
  260. }
  261. // Replace but adjust selection bounds.
  262. text_editor->insert_text_at_caret(repl_text, 0);
  263. if (match_to.x == selection_end.x) {
  264. selection_end.y += repl_text.length() - search_text_len;
  265. }
  266. } else {
  267. // Just replace.
  268. text_editor->insert_text_at_caret(repl_text, 0);
  269. }
  270. rc++;
  271. } while (search_next());
  272. }
  273. text_editor->end_complex_operation();
  274. replace_all_mode = false;
  275. // Restore editor state (selection, cursor, scroll).
  276. text_editor->set_caret_line(orig_cursor.x, false, true, 0, 0);
  277. text_editor->set_caret_column(orig_cursor.y, true, 0);
  278. if (selection_enabled) {
  279. // Reselect.
  280. text_editor->select(selection_begin.x, selection_begin.y, selection_end.x, selection_end.y, 0);
  281. }
  282. text_editor->set_v_scroll(vsval);
  283. matches_label->add_theme_color_override("font_color", rc > 0 ? get_theme_color(SNAME("font_color"), SNAME("Label")) : get_theme_color(SNAME("error_color"), EditorStringName(Editor)));
  284. matches_label->set_text(vformat(TTR("%d replaced."), rc));
  285. callable_mp((Object *)text_editor, &Object::connect).call_deferred("text_changed", callable_mp(this, &FindReplaceBar::_editor_text_changed), 0U);
  286. results_count = -1;
  287. results_count_to_current = -1;
  288. needs_to_count_results = true;
  289. }
  290. void FindReplaceBar::_get_search_from(int &r_line, int &r_col, bool p_is_searching_next) {
  291. if (!text_editor->has_selection(0) || is_selection_only()) {
  292. r_line = text_editor->get_caret_line(0);
  293. r_col = text_editor->get_caret_column(0);
  294. if (!p_is_searching_next && r_line == result_line && r_col >= result_col && r_col <= result_col + get_search_text().length()) {
  295. r_col = result_col;
  296. }
  297. return;
  298. }
  299. if (p_is_searching_next) {
  300. r_line = text_editor->get_selection_to_line();
  301. r_col = text_editor->get_selection_to_column();
  302. } else {
  303. r_line = text_editor->get_selection_from_line();
  304. r_col = text_editor->get_selection_from_column();
  305. }
  306. }
  307. void FindReplaceBar::_update_results_count() {
  308. if (!needs_to_count_results && (result_line != -1) && results_count_to_current > 0) {
  309. results_count_to_current += (flags & TextEdit::SEARCH_BACKWARDS) ? -1 : 1;
  310. if (results_count_to_current > results_count) {
  311. results_count_to_current = results_count_to_current - results_count;
  312. } else if (results_count_to_current <= 0) {
  313. results_count_to_current = results_count;
  314. }
  315. return;
  316. }
  317. String searched = get_search_text();
  318. if (searched.is_empty()) {
  319. return;
  320. }
  321. needs_to_count_results = false;
  322. results_count = 0;
  323. for (int i = 0; i < text_editor->get_line_count(); i++) {
  324. String line_text = text_editor->get_line(i);
  325. int col_pos = 0;
  326. bool searched_start_is_symbol = is_symbol(searched[0]);
  327. bool searched_end_is_symbol = is_symbol(searched[searched.length() - 1]);
  328. while (true) {
  329. col_pos = is_case_sensitive() ? line_text.find(searched, col_pos) : line_text.findn(searched, col_pos);
  330. if (col_pos == -1) {
  331. break;
  332. }
  333. if (is_whole_words()) {
  334. if (!searched_start_is_symbol && col_pos > 0 && !is_symbol(line_text[col_pos - 1])) {
  335. col_pos += searched.length();
  336. continue;
  337. }
  338. if (!searched_end_is_symbol && col_pos + searched.length() < line_text.length() && !is_symbol(line_text[col_pos + searched.length()])) {
  339. col_pos += searched.length();
  340. continue;
  341. }
  342. }
  343. results_count++;
  344. if (i == result_line) {
  345. if (col_pos == result_col) {
  346. results_count_to_current = results_count;
  347. } else if (col_pos < result_col && col_pos + searched.length() > result_col) {
  348. col_pos = result_col;
  349. results_count_to_current = results_count;
  350. }
  351. }
  352. col_pos += searched.length();
  353. }
  354. }
  355. }
  356. void FindReplaceBar::_update_matches_display() {
  357. if (search_text->get_text().is_empty() || results_count == -1) {
  358. matches_label->hide();
  359. } else {
  360. matches_label->show();
  361. matches_label->add_theme_color_override("font_color", results_count > 0 ? get_theme_color(SNAME("font_color"), SNAME("Label")) : get_theme_color(SNAME("error_color"), EditorStringName(Editor)));
  362. if (results_count == 0) {
  363. matches_label->set_text(TTR("No match"));
  364. } else if (results_count_to_current == -1) {
  365. matches_label->set_text(vformat(TTRN("%d match", "%d matches", results_count), results_count));
  366. } else {
  367. matches_label->set_text(vformat(TTRN("%d of %d match", "%d of %d matches", results_count), results_count_to_current, results_count));
  368. }
  369. }
  370. find_prev->set_disabled(results_count < 1);
  371. find_next->set_disabled(results_count < 1);
  372. replace->set_disabled(search_text->get_text().is_empty());
  373. replace_all->set_disabled(search_text->get_text().is_empty());
  374. }
  375. bool FindReplaceBar::search_current() {
  376. _update_flags(false);
  377. int line, col;
  378. _get_search_from(line, col);
  379. return _search(flags, line, col);
  380. }
  381. bool FindReplaceBar::search_prev() {
  382. if (is_selection_only() && !replace_all_mode) {
  383. return false;
  384. }
  385. if (!is_visible()) {
  386. popup_search(true);
  387. }
  388. String text = get_search_text();
  389. _update_flags(true);
  390. int line, col;
  391. _get_search_from(line, col);
  392. col -= text.length();
  393. if (col < 0) {
  394. line -= 1;
  395. if (line < 0) {
  396. line = text_editor->get_line_count() - 1;
  397. }
  398. col = text_editor->get_line(line).length();
  399. }
  400. return _search(flags, line, col);
  401. }
  402. bool FindReplaceBar::search_next() {
  403. if (is_selection_only() && !replace_all_mode) {
  404. return false;
  405. }
  406. if (!is_visible()) {
  407. popup_search(true);
  408. }
  409. _update_flags(false);
  410. int line, col;
  411. _get_search_from(line, col, true);
  412. return _search(flags, line, col);
  413. }
  414. void FindReplaceBar::_hide_bar(bool p_force_focus) {
  415. if (replace_text->has_focus() || search_text->has_focus() || p_force_focus) {
  416. text_editor->grab_focus();
  417. }
  418. text_editor->set_search_text("");
  419. result_line = -1;
  420. result_col = -1;
  421. hide();
  422. }
  423. void FindReplaceBar::_show_search(bool p_with_replace, bool p_show_only) {
  424. show();
  425. if (p_show_only) {
  426. return;
  427. }
  428. const bool on_one_line = text_editor->has_selection(0) && text_editor->get_selection_from_line(0) == text_editor->get_selection_to_line(0);
  429. const bool focus_replace = p_with_replace && on_one_line;
  430. if (focus_replace) {
  431. search_text->deselect();
  432. callable_mp((Control *)replace_text, &Control::grab_focus).call_deferred();
  433. } else {
  434. replace_text->deselect();
  435. callable_mp((Control *)search_text, &Control::grab_focus).call_deferred();
  436. }
  437. if (on_one_line) {
  438. search_text->set_text(text_editor->get_selected_text(0));
  439. result_line = text_editor->get_selection_from_line();
  440. result_col = text_editor->get_selection_from_column();
  441. }
  442. if (!get_search_text().is_empty()) {
  443. if (focus_replace) {
  444. replace_text->select_all();
  445. replace_text->set_caret_column(replace_text->get_text().length());
  446. } else {
  447. search_text->select_all();
  448. search_text->set_caret_column(search_text->get_text().length());
  449. }
  450. preserve_cursor = true;
  451. _search_text_changed(get_search_text());
  452. preserve_cursor = false;
  453. }
  454. }
  455. void FindReplaceBar::popup_search(bool p_show_only) {
  456. replace_text->hide();
  457. hbc_button_replace->hide();
  458. hbc_option_replace->hide();
  459. selection_only->set_pressed(false);
  460. _show_search(false, p_show_only);
  461. }
  462. void FindReplaceBar::popup_replace() {
  463. if (!replace_text->is_visible_in_tree()) {
  464. replace_text->show();
  465. hbc_button_replace->show();
  466. hbc_option_replace->show();
  467. }
  468. selection_only->set_pressed(text_editor->has_selection(0) && text_editor->get_selection_from_line(0) < text_editor->get_selection_to_line(0));
  469. _show_search(true, false);
  470. }
  471. void FindReplaceBar::_search_options_changed(bool p_pressed) {
  472. results_count = -1;
  473. results_count_to_current = -1;
  474. needs_to_count_results = true;
  475. search_current();
  476. }
  477. void FindReplaceBar::_editor_text_changed() {
  478. results_count = -1;
  479. results_count_to_current = -1;
  480. needs_to_count_results = true;
  481. if (is_visible_in_tree()) {
  482. preserve_cursor = true;
  483. search_current();
  484. preserve_cursor = false;
  485. }
  486. }
  487. void FindReplaceBar::_search_text_changed(const String &p_text) {
  488. results_count = -1;
  489. results_count_to_current = -1;
  490. needs_to_count_results = true;
  491. search_current();
  492. }
  493. void FindReplaceBar::_search_text_submitted(const String &p_text) {
  494. if (Input::get_singleton()->is_key_pressed(Key::SHIFT)) {
  495. search_prev();
  496. } else {
  497. search_next();
  498. }
  499. }
  500. void FindReplaceBar::_replace_text_submitted(const String &p_text) {
  501. if (selection_only->is_pressed() && text_editor->has_selection(0)) {
  502. _replace_all();
  503. _hide_bar();
  504. } else if (Input::get_singleton()->is_key_pressed(Key::SHIFT)) {
  505. _replace();
  506. search_prev();
  507. } else {
  508. _replace();
  509. search_next();
  510. }
  511. }
  512. String FindReplaceBar::get_search_text() const {
  513. return search_text->get_text();
  514. }
  515. String FindReplaceBar::get_replace_text() const {
  516. return replace_text->get_text();
  517. }
  518. bool FindReplaceBar::is_case_sensitive() const {
  519. return case_sensitive->is_pressed();
  520. }
  521. bool FindReplaceBar::is_whole_words() const {
  522. return whole_words->is_pressed();
  523. }
  524. bool FindReplaceBar::is_selection_only() const {
  525. return selection_only->is_pressed();
  526. }
  527. void FindReplaceBar::set_error(const String &p_label) {
  528. emit_signal(SNAME("error"), p_label);
  529. }
  530. void FindReplaceBar::set_text_edit(CodeTextEditor *p_text_editor) {
  531. if (p_text_editor == base_text_editor) {
  532. return;
  533. }
  534. if (base_text_editor) {
  535. base_text_editor->remove_find_replace_bar();
  536. base_text_editor = nullptr;
  537. text_editor->disconnect("text_changed", callable_mp(this, &FindReplaceBar::_editor_text_changed));
  538. text_editor = nullptr;
  539. }
  540. if (!p_text_editor) {
  541. return;
  542. }
  543. results_count = -1;
  544. results_count_to_current = -1;
  545. needs_to_count_results = true;
  546. base_text_editor = p_text_editor;
  547. text_editor = base_text_editor->get_text_editor();
  548. text_editor->connect("text_changed", callable_mp(this, &FindReplaceBar::_editor_text_changed));
  549. _update_results_count();
  550. _update_matches_display();
  551. }
  552. void FindReplaceBar::_bind_methods() {
  553. ClassDB::bind_method("_search_current", &FindReplaceBar::search_current);
  554. ADD_SIGNAL(MethodInfo("error"));
  555. }
  556. FindReplaceBar::FindReplaceBar() {
  557. vbc_lineedit = memnew(VBoxContainer);
  558. add_child(vbc_lineedit);
  559. vbc_lineedit->set_alignment(BoxContainer::ALIGNMENT_CENTER);
  560. vbc_lineedit->set_h_size_flags(SIZE_EXPAND_FILL);
  561. VBoxContainer *vbc_button = memnew(VBoxContainer);
  562. add_child(vbc_button);
  563. VBoxContainer *vbc_option = memnew(VBoxContainer);
  564. add_child(vbc_option);
  565. HBoxContainer *hbc_button_search = memnew(HBoxContainer);
  566. vbc_button->add_child(hbc_button_search);
  567. hbc_button_search->set_alignment(BoxContainer::ALIGNMENT_END);
  568. hbc_button_replace = memnew(HBoxContainer);
  569. vbc_button->add_child(hbc_button_replace);
  570. hbc_button_replace->set_alignment(BoxContainer::ALIGNMENT_END);
  571. HBoxContainer *hbc_option_search = memnew(HBoxContainer);
  572. vbc_option->add_child(hbc_option_search);
  573. hbc_option_replace = memnew(HBoxContainer);
  574. vbc_option->add_child(hbc_option_replace);
  575. // Search toolbar
  576. search_text = memnew(LineEdit);
  577. vbc_lineedit->add_child(search_text);
  578. search_text->set_placeholder(TTR("Find"));
  579. search_text->set_tooltip_text(TTR("Find"));
  580. search_text->set_custom_minimum_size(Size2(100 * EDSCALE, 0));
  581. search_text->connect("text_changed", callable_mp(this, &FindReplaceBar::_search_text_changed));
  582. search_text->connect("text_submitted", callable_mp(this, &FindReplaceBar::_search_text_submitted));
  583. search_text->connect(SceneStringName(focus_exited), callable_mp(this, &FindReplaceBar::_focus_lost));
  584. matches_label = memnew(Label);
  585. hbc_button_search->add_child(matches_label);
  586. matches_label->hide();
  587. find_prev = memnew(Button);
  588. find_prev->set_flat(true);
  589. find_prev->set_tooltip_text(TTR("Previous Match"));
  590. hbc_button_search->add_child(find_prev);
  591. find_prev->set_focus_mode(FOCUS_NONE);
  592. find_prev->connect(SceneStringName(pressed), callable_mp(this, &FindReplaceBar::search_prev));
  593. find_next = memnew(Button);
  594. find_next->set_flat(true);
  595. find_next->set_tooltip_text(TTR("Next Match"));
  596. hbc_button_search->add_child(find_next);
  597. find_next->set_focus_mode(FOCUS_NONE);
  598. find_next->connect(SceneStringName(pressed), callable_mp(this, &FindReplaceBar::search_next));
  599. case_sensitive = memnew(CheckBox);
  600. hbc_option_search->add_child(case_sensitive);
  601. case_sensitive->set_text(TTR("Match Case"));
  602. case_sensitive->set_focus_mode(FOCUS_NONE);
  603. case_sensitive->connect("toggled", callable_mp(this, &FindReplaceBar::_search_options_changed));
  604. whole_words = memnew(CheckBox);
  605. hbc_option_search->add_child(whole_words);
  606. whole_words->set_text(TTR("Whole Words"));
  607. whole_words->set_focus_mode(FOCUS_NONE);
  608. whole_words->connect("toggled", callable_mp(this, &FindReplaceBar::_search_options_changed));
  609. // Replace toolbar
  610. replace_text = memnew(LineEdit);
  611. vbc_lineedit->add_child(replace_text);
  612. replace_text->set_placeholder(TTR("Replace"));
  613. replace_text->set_tooltip_text(TTR("Replace"));
  614. replace_text->set_custom_minimum_size(Size2(100 * EDSCALE, 0));
  615. replace_text->connect("text_submitted", callable_mp(this, &FindReplaceBar::_replace_text_submitted));
  616. replace_text->connect(SceneStringName(focus_exited), callable_mp(this, &FindReplaceBar::_focus_lost));
  617. replace = memnew(Button);
  618. hbc_button_replace->add_child(replace);
  619. replace->set_text(TTR("Replace"));
  620. replace->connect(SceneStringName(pressed), callable_mp(this, &FindReplaceBar::_replace));
  621. replace_all = memnew(Button);
  622. hbc_button_replace->add_child(replace_all);
  623. replace_all->set_text(TTR("Replace All"));
  624. replace_all->connect(SceneStringName(pressed), callable_mp(this, &FindReplaceBar::_replace_all));
  625. selection_only = memnew(CheckBox);
  626. hbc_option_replace->add_child(selection_only);
  627. selection_only->set_text(TTR("Selection Only"));
  628. selection_only->set_focus_mode(FOCUS_NONE);
  629. selection_only->connect("toggled", callable_mp(this, &FindReplaceBar::_search_options_changed));
  630. hide_button = memnew(TextureButton);
  631. add_child(hide_button);
  632. hide_button->set_tooltip_text(TTR("Hide"));
  633. hide_button->set_focus_mode(FOCUS_NONE);
  634. hide_button->connect(SceneStringName(pressed), callable_mp(this, &FindReplaceBar::_hide_bar).bind(false));
  635. hide_button->set_v_size_flags(SIZE_SHRINK_CENTER);
  636. }
  637. /*** CODE EDITOR ****/
  638. static constexpr float ZOOM_FACTOR_PRESETS[7] = { 0.25f, 0.5f, 0.75f, 1.0f, 1.5f, 2.0f, 3.0f };
  639. // This function should be used to handle shortcuts that could otherwise
  640. // be handled too late if they weren't handled here.
  641. void CodeTextEditor::input(const Ref<InputEvent> &event) {
  642. ERR_FAIL_COND(event.is_null());
  643. const Ref<InputEventKey> key_event = event;
  644. if (!key_event.is_valid()) {
  645. return;
  646. }
  647. if (!key_event->is_pressed()) {
  648. return;
  649. }
  650. if (!text_editor->has_focus()) {
  651. if ((find_replace_bar != nullptr && find_replace_bar->is_visible()) && (find_replace_bar->has_focus() || (get_viewport()->gui_get_focus_owner() && find_replace_bar->is_ancestor_of(get_viewport()->gui_get_focus_owner())))) {
  652. if (ED_IS_SHORTCUT("script_text_editor/find_next", key_event)) {
  653. find_replace_bar->search_next();
  654. accept_event();
  655. return;
  656. }
  657. if (ED_IS_SHORTCUT("script_text_editor/find_previous", key_event)) {
  658. find_replace_bar->search_prev();
  659. accept_event();
  660. return;
  661. }
  662. }
  663. return;
  664. }
  665. if (ED_IS_SHORTCUT("script_text_editor/move_up", key_event)) {
  666. text_editor->move_lines_up();
  667. accept_event();
  668. return;
  669. }
  670. if (ED_IS_SHORTCUT("script_text_editor/move_down", key_event)) {
  671. text_editor->move_lines_down();
  672. accept_event();
  673. return;
  674. }
  675. if (ED_IS_SHORTCUT("script_text_editor/delete_line", key_event)) {
  676. text_editor->delete_lines();
  677. accept_event();
  678. return;
  679. }
  680. if (ED_IS_SHORTCUT("script_text_editor/duplicate_selection", key_event)) {
  681. text_editor->duplicate_selection();
  682. accept_event();
  683. return;
  684. }
  685. if (ED_IS_SHORTCUT("script_text_editor/duplicate_lines", key_event)) {
  686. text_editor->duplicate_lines();
  687. accept_event();
  688. return;
  689. }
  690. }
  691. void CodeTextEditor::_text_editor_gui_input(const Ref<InputEvent> &p_event) {
  692. Ref<InputEventMouseButton> mb = p_event;
  693. if (mb.is_valid()) {
  694. if (mb->is_pressed() && mb->is_command_or_control_pressed()) {
  695. if (mb->get_button_index() == MouseButton::WHEEL_UP) {
  696. _zoom_in();
  697. accept_event();
  698. return;
  699. }
  700. if (mb->get_button_index() == MouseButton::WHEEL_DOWN) {
  701. _zoom_out();
  702. accept_event();
  703. return;
  704. }
  705. }
  706. }
  707. Ref<InputEventMagnifyGesture> magnify_gesture = p_event;
  708. if (magnify_gesture.is_valid()) {
  709. _zoom_to(zoom_factor * powf(magnify_gesture->get_factor(), 0.25f));
  710. accept_event();
  711. return;
  712. }
  713. Ref<InputEventKey> k = p_event;
  714. if (k.is_valid()) {
  715. if (k->is_pressed()) {
  716. if (ED_IS_SHORTCUT("script_editor/zoom_in", p_event)) {
  717. _zoom_in();
  718. accept_event();
  719. return;
  720. }
  721. if (ED_IS_SHORTCUT("script_editor/zoom_out", p_event)) {
  722. _zoom_out();
  723. accept_event();
  724. return;
  725. }
  726. if (ED_IS_SHORTCUT("script_editor/reset_zoom", p_event)) {
  727. _zoom_to(1);
  728. accept_event();
  729. return;
  730. }
  731. }
  732. }
  733. }
  734. void CodeTextEditor::_line_col_changed() {
  735. if (!code_complete_timer->is_stopped() && code_complete_timer_line != text_editor->get_caret_line()) {
  736. code_complete_timer->stop();
  737. }
  738. String line = text_editor->get_line(text_editor->get_caret_line());
  739. int positional_column = 0;
  740. for (int i = 0; i < text_editor->get_caret_column(); i++) {
  741. if (line[i] == '\t') {
  742. positional_column += text_editor->get_indent_size(); // Tab size
  743. } else {
  744. positional_column += 1;
  745. }
  746. }
  747. StringBuilder sb;
  748. sb.append(itos(text_editor->get_caret_line() + 1).lpad(4));
  749. sb.append(" : ");
  750. sb.append(itos(positional_column + 1).lpad(3));
  751. line_and_col_txt->set_text(sb.as_string());
  752. if (find_replace_bar) {
  753. if (!find_replace_bar->line_col_changed_for_result) {
  754. find_replace_bar->needs_to_count_results = true;
  755. }
  756. find_replace_bar->line_col_changed_for_result = false;
  757. }
  758. }
  759. void CodeTextEditor::_text_changed() {
  760. if (code_complete_enabled && text_editor->is_insert_text_operation()) {
  761. code_complete_timer_line = text_editor->get_caret_line();
  762. code_complete_timer->start();
  763. }
  764. idle->start();
  765. if (find_replace_bar) {
  766. find_replace_bar->needs_to_count_results = true;
  767. }
  768. }
  769. void CodeTextEditor::_code_complete_timer_timeout() {
  770. if (!is_visible_in_tree()) {
  771. return;
  772. }
  773. text_editor->request_code_completion();
  774. }
  775. void CodeTextEditor::_complete_request() {
  776. List<ScriptLanguage::CodeCompletionOption> entries;
  777. String ctext = text_editor->get_text_for_code_completion();
  778. _code_complete_script(ctext, &entries);
  779. bool forced = false;
  780. if (code_complete_func) {
  781. code_complete_func(code_complete_ud, ctext, &entries, forced);
  782. }
  783. for (const ScriptLanguage::CodeCompletionOption &e : entries) {
  784. Color font_color = completion_font_color;
  785. if (!e.theme_color_name.is_empty() && EDITOR_GET("text_editor/completion/colorize_suggestions")) {
  786. font_color = get_theme_color(e.theme_color_name, SNAME("Editor"));
  787. } else if (e.insert_text.begins_with("\"") || e.insert_text.begins_with("\'")) {
  788. font_color = completion_string_color;
  789. } else if (e.insert_text.begins_with("##") || e.insert_text.begins_with("///")) {
  790. font_color = completion_doc_comment_color;
  791. } else if (e.insert_text.begins_with("&")) {
  792. font_color = completion_string_name_color;
  793. } else if (e.insert_text.begins_with("^")) {
  794. font_color = completion_node_path_color;
  795. } else if (e.insert_text.begins_with("#") || e.insert_text.begins_with("//")) {
  796. font_color = completion_comment_color;
  797. }
  798. text_editor->add_code_completion_option((CodeEdit::CodeCompletionKind)e.kind, e.display, e.insert_text, font_color, _get_completion_icon(e), e.default_value, e.location);
  799. }
  800. text_editor->update_code_completion_options(forced);
  801. }
  802. Ref<Texture2D> CodeTextEditor::_get_completion_icon(const ScriptLanguage::CodeCompletionOption &p_option) {
  803. Ref<Texture2D> tex;
  804. switch (p_option.kind) {
  805. case ScriptLanguage::CODE_COMPLETION_KIND_CLASS: {
  806. if (has_theme_icon(p_option.display, EditorStringName(EditorIcons))) {
  807. tex = get_editor_theme_icon(p_option.display);
  808. } else {
  809. tex = EditorNode::get_singleton()->get_class_icon(p_option.display);
  810. }
  811. } break;
  812. case ScriptLanguage::CODE_COMPLETION_KIND_ENUM:
  813. tex = get_editor_theme_icon(SNAME("Enum"));
  814. break;
  815. case ScriptLanguage::CODE_COMPLETION_KIND_FILE_PATH:
  816. tex = get_editor_theme_icon(SNAME("File"));
  817. break;
  818. case ScriptLanguage::CODE_COMPLETION_KIND_NODE_PATH:
  819. tex = get_editor_theme_icon(SNAME("NodePath"));
  820. break;
  821. case ScriptLanguage::CODE_COMPLETION_KIND_VARIABLE:
  822. tex = get_editor_theme_icon(SNAME("Variant"));
  823. break;
  824. case ScriptLanguage::CODE_COMPLETION_KIND_CONSTANT:
  825. tex = get_editor_theme_icon(SNAME("MemberConstant"));
  826. break;
  827. case ScriptLanguage::CODE_COMPLETION_KIND_MEMBER:
  828. tex = get_editor_theme_icon(SNAME("MemberProperty"));
  829. break;
  830. case ScriptLanguage::CODE_COMPLETION_KIND_SIGNAL:
  831. tex = get_editor_theme_icon(SNAME("MemberSignal"));
  832. break;
  833. case ScriptLanguage::CODE_COMPLETION_KIND_FUNCTION:
  834. tex = get_editor_theme_icon(SNAME("MemberMethod"));
  835. break;
  836. case ScriptLanguage::CODE_COMPLETION_KIND_PLAIN_TEXT:
  837. tex = get_editor_theme_icon(SNAME("BoxMesh"));
  838. break;
  839. default:
  840. tex = get_editor_theme_icon(SNAME("String"));
  841. break;
  842. }
  843. return tex;
  844. }
  845. void CodeTextEditor::update_editor_settings() {
  846. // Theme: Highlighting
  847. completion_font_color = EDITOR_GET("text_editor/theme/highlighting/completion_font_color");
  848. completion_string_color = EDITOR_GET("text_editor/theme/highlighting/string_color");
  849. completion_string_name_color = EDITOR_GET("text_editor/theme/highlighting/gdscript/string_name_color");
  850. completion_node_path_color = EDITOR_GET("text_editor/theme/highlighting/gdscript/node_path_color");
  851. completion_comment_color = EDITOR_GET("text_editor/theme/highlighting/comment_color");
  852. completion_doc_comment_color = EDITOR_GET("text_editor/theme/highlighting/doc_comment_color");
  853. // Appearance: Caret
  854. text_editor->set_caret_type((TextEdit::CaretType)EDITOR_GET("text_editor/appearance/caret/type").operator int());
  855. text_editor->set_caret_blink_enabled(EDITOR_GET("text_editor/appearance/caret/caret_blink"));
  856. text_editor->set_caret_blink_interval(EDITOR_GET("text_editor/appearance/caret/caret_blink_interval"));
  857. text_editor->set_highlight_current_line(EDITOR_GET("text_editor/appearance/caret/highlight_current_line"));
  858. text_editor->set_highlight_all_occurrences(EDITOR_GET("text_editor/appearance/caret/highlight_all_occurrences"));
  859. // Appearance: Gutters
  860. text_editor->set_draw_line_numbers(EDITOR_GET("text_editor/appearance/gutters/show_line_numbers"));
  861. text_editor->set_line_numbers_zero_padded(EDITOR_GET("text_editor/appearance/gutters/line_numbers_zero_padded"));
  862. // Appearance: Minimap
  863. text_editor->set_draw_minimap(EDITOR_GET("text_editor/appearance/minimap/show_minimap"));
  864. text_editor->set_minimap_width((int)EDITOR_GET("text_editor/appearance/minimap/minimap_width") * EDSCALE);
  865. // Appearance: Lines
  866. text_editor->set_line_folding_enabled(EDITOR_GET("text_editor/appearance/lines/code_folding"));
  867. text_editor->set_draw_fold_gutter(EDITOR_GET("text_editor/appearance/lines/code_folding"));
  868. text_editor->set_line_wrapping_mode((TextEdit::LineWrappingMode)EDITOR_GET("text_editor/appearance/lines/word_wrap").operator int());
  869. text_editor->set_autowrap_mode((TextServer::AutowrapMode)EDITOR_GET("text_editor/appearance/lines/autowrap_mode").operator int());
  870. // Appearance: Whitespace
  871. text_editor->set_draw_tabs(EDITOR_GET("text_editor/appearance/whitespace/draw_tabs"));
  872. text_editor->set_draw_spaces(EDITOR_GET("text_editor/appearance/whitespace/draw_spaces"));
  873. text_editor->add_theme_constant_override("line_spacing", EDITOR_GET("text_editor/appearance/whitespace/line_spacing"));
  874. // Behavior: Navigation
  875. text_editor->set_scroll_past_end_of_file_enabled(EDITOR_GET("text_editor/behavior/navigation/scroll_past_end_of_file"));
  876. text_editor->set_smooth_scroll_enabled(EDITOR_GET("text_editor/behavior/navigation/smooth_scrolling"));
  877. text_editor->set_v_scroll_speed(EDITOR_GET("text_editor/behavior/navigation/v_scroll_speed"));
  878. text_editor->set_drag_and_drop_selection_enabled(EDITOR_GET("text_editor/behavior/navigation/drag_and_drop_selection"));
  879. // Behavior: Indent
  880. set_indent_using_spaces(EDITOR_GET("text_editor/behavior/indent/type"));
  881. text_editor->set_indent_size(EDITOR_GET("text_editor/behavior/indent/size"));
  882. text_editor->set_auto_indent_enabled(EDITOR_GET("text_editor/behavior/indent/auto_indent"));
  883. text_editor->set_indent_wrapped_lines(EDITOR_GET("text_editor/behavior/indent/indent_wrapped_lines"));
  884. // Completion
  885. text_editor->set_auto_brace_completion_enabled(EDITOR_GET("text_editor/completion/auto_brace_complete"));
  886. text_editor->set_code_hint_draw_below(EDITOR_GET("text_editor/completion/put_callhint_tooltip_below_current_line"));
  887. code_complete_enabled = EDITOR_GET("text_editor/completion/code_complete_enabled");
  888. code_complete_timer->set_wait_time(EDITOR_GET("text_editor/completion/code_complete_delay"));
  889. idle->set_wait_time(EDITOR_GET("text_editor/completion/idle_parse_delay"));
  890. // Appearance: Guidelines
  891. if (EDITOR_GET("text_editor/appearance/guidelines/show_line_length_guidelines")) {
  892. TypedArray<int> guideline_cols;
  893. guideline_cols.append(EDITOR_GET("text_editor/appearance/guidelines/line_length_guideline_hard_column"));
  894. if (EDITOR_GET("text_editor/appearance/guidelines/line_length_guideline_soft_column") != guideline_cols[0]) {
  895. guideline_cols.append(EDITOR_GET("text_editor/appearance/guidelines/line_length_guideline_soft_column"));
  896. }
  897. text_editor->set_line_length_guidelines(guideline_cols);
  898. } else {
  899. text_editor->set_line_length_guidelines(TypedArray<int>());
  900. }
  901. set_zoom_factor(zoom_factor);
  902. }
  903. void CodeTextEditor::set_find_replace_bar(FindReplaceBar *p_bar) {
  904. if (find_replace_bar) {
  905. return;
  906. }
  907. find_replace_bar = p_bar;
  908. find_replace_bar->set_text_edit(this);
  909. find_replace_bar->connect("error", callable_mp(error, &Label::set_text));
  910. }
  911. void CodeTextEditor::remove_find_replace_bar() {
  912. if (!find_replace_bar) {
  913. return;
  914. }
  915. find_replace_bar->disconnect("error", callable_mp(error, &Label::set_text));
  916. find_replace_bar = nullptr;
  917. }
  918. void CodeTextEditor::trim_trailing_whitespace() {
  919. bool trimmed_whitespace = false;
  920. for (int i = 0; i < text_editor->get_line_count(); i++) {
  921. String line = text_editor->get_line(i);
  922. if (line.ends_with(" ") || line.ends_with("\t")) {
  923. if (!trimmed_whitespace) {
  924. text_editor->begin_complex_operation();
  925. trimmed_whitespace = true;
  926. }
  927. int end = 0;
  928. for (int j = line.length() - 1; j > -1; j--) {
  929. if (line[j] != ' ' && line[j] != '\t') {
  930. end = j + 1;
  931. break;
  932. }
  933. }
  934. text_editor->remove_text(i, end, i, line.length());
  935. }
  936. }
  937. if (trimmed_whitespace) {
  938. text_editor->merge_overlapping_carets();
  939. text_editor->end_complex_operation();
  940. }
  941. }
  942. void CodeTextEditor::insert_final_newline() {
  943. int final_line = text_editor->get_line_count() - 1;
  944. String line = text_editor->get_line(final_line);
  945. // Length 0 means it's already an empty line, no need to add a newline.
  946. if (line.length() > 0 && !line.ends_with("\n")) {
  947. text_editor->insert_text("\n", final_line, line.length(), false);
  948. }
  949. }
  950. void CodeTextEditor::convert_case(CaseStyle p_case) {
  951. if (!text_editor->has_selection()) {
  952. return;
  953. }
  954. text_editor->begin_complex_operation();
  955. text_editor->begin_multicaret_edit();
  956. for (int c = 0; c < text_editor->get_caret_count(); c++) {
  957. if (text_editor->multicaret_edit_ignore_caret(c)) {
  958. continue;
  959. }
  960. if (!text_editor->has_selection(c)) {
  961. continue;
  962. }
  963. int begin = text_editor->get_selection_from_line(c);
  964. int end = text_editor->get_selection_to_line(c);
  965. int begin_col = text_editor->get_selection_from_column(c);
  966. int end_col = text_editor->get_selection_to_column(c);
  967. for (int i = begin; i <= end; i++) {
  968. int len = text_editor->get_line(i).length();
  969. if (i == end) {
  970. len = end_col;
  971. }
  972. if (i == begin) {
  973. len -= begin_col;
  974. }
  975. String new_line = text_editor->get_line(i).substr(i == begin ? begin_col : 0, len);
  976. switch (p_case) {
  977. case UPPER: {
  978. new_line = new_line.to_upper();
  979. } break;
  980. case LOWER: {
  981. new_line = new_line.to_lower();
  982. } break;
  983. case CAPITALIZE: {
  984. new_line = new_line.capitalize();
  985. } break;
  986. }
  987. if (i == begin) {
  988. new_line = text_editor->get_line(i).left(begin_col) + new_line;
  989. }
  990. if (i == end) {
  991. new_line = new_line + text_editor->get_line(i).substr(end_col);
  992. }
  993. text_editor->set_line(i, new_line);
  994. }
  995. }
  996. text_editor->end_multicaret_edit();
  997. text_editor->end_complex_operation();
  998. }
  999. void CodeTextEditor::set_indent_using_spaces(bool p_use_spaces) {
  1000. text_editor->set_indent_using_spaces(p_use_spaces);
  1001. indentation_txt->set_text(p_use_spaces ? TTR("Spaces", "Indentation") : TTR("Tabs", "Indentation"));
  1002. }
  1003. void CodeTextEditor::toggle_inline_comment(const String &delimiter) {
  1004. text_editor->begin_complex_operation();
  1005. text_editor->begin_multicaret_edit();
  1006. Vector<Point2i> line_ranges = text_editor->get_line_ranges_from_carets();
  1007. int folded_to = 0;
  1008. for (Point2i line_range : line_ranges) {
  1009. int from_line = line_range.x;
  1010. int to_line = line_range.y;
  1011. // If last line is folded, extends to the end of the folded section
  1012. if (text_editor->is_line_folded(to_line)) {
  1013. folded_to = text_editor->get_next_visible_line_offset_from(to_line + 1, 1) - 1;
  1014. to_line += folded_to;
  1015. }
  1016. // Check first if there's any uncommented lines in selection.
  1017. bool is_commented = true;
  1018. bool is_all_empty = true;
  1019. for (int line = from_line; line <= to_line; line++) {
  1020. // `+ delimiter.length()` here because comment delimiter is not actually `in comment` so we check first character after it
  1021. int delimiter_idx = text_editor->is_in_comment(line, text_editor->get_first_non_whitespace_column(line) + delimiter.length());
  1022. // Empty lines should not be counted.
  1023. bool is_empty = text_editor->get_line(line).strip_edges().is_empty();
  1024. is_all_empty = is_all_empty && is_empty;
  1025. // get_delimiter_start_key will return `##` instead of `#` when there is multiple comment delimiter in a line.
  1026. if (!is_empty && (delimiter_idx == -1 || !text_editor->get_delimiter_start_key(delimiter_idx).begins_with(delimiter))) {
  1027. is_commented = false;
  1028. break;
  1029. }
  1030. }
  1031. // Special case for commenting empty lines, treat it/them as uncommented lines.
  1032. is_commented = is_commented && !is_all_empty;
  1033. // Comment/uncomment.
  1034. for (int line = from_line; line <= to_line; line++) {
  1035. if (is_all_empty) {
  1036. text_editor->insert_text(delimiter, line, 0);
  1037. continue;
  1038. }
  1039. if (is_commented) {
  1040. int delimiter_column = text_editor->get_line(line).find(delimiter);
  1041. text_editor->remove_text(line, delimiter_column, line, delimiter_column + delimiter.length());
  1042. } else {
  1043. text_editor->insert_text(delimiter, line, text_editor->get_first_non_whitespace_column(line));
  1044. }
  1045. }
  1046. }
  1047. text_editor->end_multicaret_edit();
  1048. text_editor->end_complex_operation();
  1049. }
  1050. void CodeTextEditor::goto_line(int p_line) {
  1051. text_editor->remove_secondary_carets();
  1052. text_editor->deselect();
  1053. text_editor->unfold_line(p_line);
  1054. callable_mp((TextEdit *)text_editor, &TextEdit::set_caret_line).call_deferred(p_line, true, true, 0, 0);
  1055. }
  1056. void CodeTextEditor::goto_line_selection(int p_line, int p_begin, int p_end) {
  1057. text_editor->remove_secondary_carets();
  1058. text_editor->unfold_line(p_line);
  1059. callable_mp((TextEdit *)text_editor, &TextEdit::set_caret_line).call_deferred(p_line, true, true, 0, 0);
  1060. callable_mp((TextEdit *)text_editor, &TextEdit::set_caret_column).call_deferred(p_begin, true, 0);
  1061. text_editor->select(p_line, p_begin, p_line, p_end);
  1062. }
  1063. void CodeTextEditor::goto_line_centered(int p_line) {
  1064. goto_line(p_line);
  1065. callable_mp((TextEdit *)text_editor, &TextEdit::center_viewport_to_caret).call_deferred(0);
  1066. }
  1067. void CodeTextEditor::set_executing_line(int p_line) {
  1068. text_editor->set_line_as_executing(p_line, true);
  1069. }
  1070. void CodeTextEditor::clear_executing_line() {
  1071. text_editor->clear_executing_lines();
  1072. }
  1073. Variant CodeTextEditor::get_edit_state() {
  1074. Dictionary state;
  1075. state.merge(get_navigation_state());
  1076. state["folded_lines"] = text_editor->get_folded_lines();
  1077. state["breakpoints"] = text_editor->get_breakpointed_lines();
  1078. state["bookmarks"] = text_editor->get_bookmarked_lines();
  1079. Ref<EditorSyntaxHighlighter> syntax_highlighter = text_editor->get_syntax_highlighter();
  1080. state["syntax_highlighter"] = syntax_highlighter->_get_name();
  1081. return state;
  1082. }
  1083. Variant CodeTextEditor::get_previous_state() {
  1084. return previous_state;
  1085. }
  1086. void CodeTextEditor::store_previous_state() {
  1087. previous_state = get_navigation_state();
  1088. }
  1089. void CodeTextEditor::set_edit_state(const Variant &p_state) {
  1090. Dictionary state = p_state;
  1091. /* update the row first as it sets the column to 0 */
  1092. text_editor->set_caret_line(state["row"]);
  1093. text_editor->set_caret_column(state["column"]);
  1094. if (int(state["scroll_position"]) == -1) {
  1095. // Special case for previous state.
  1096. text_editor->center_viewport_to_caret();
  1097. } else {
  1098. text_editor->set_v_scroll(state["scroll_position"]);
  1099. }
  1100. text_editor->set_h_scroll(state["h_scroll_position"]);
  1101. if (state.get("selection", false)) {
  1102. text_editor->select(state["selection_from_line"], state["selection_from_column"], state["selection_to_line"], state["selection_to_column"]);
  1103. } else {
  1104. text_editor->deselect();
  1105. }
  1106. if (state.has("folded_lines")) {
  1107. Vector<int> folded_lines = state["folded_lines"];
  1108. for (int i = 0; i < folded_lines.size(); i++) {
  1109. text_editor->fold_line(folded_lines[i]);
  1110. }
  1111. }
  1112. if (state.has("breakpoints")) {
  1113. Array breakpoints = state["breakpoints"];
  1114. for (int i = 0; i < breakpoints.size(); i++) {
  1115. text_editor->set_line_as_breakpoint(breakpoints[i], true);
  1116. }
  1117. }
  1118. if (state.has("bookmarks")) {
  1119. Array bookmarks = state["bookmarks"];
  1120. for (int i = 0; i < bookmarks.size(); i++) {
  1121. text_editor->set_line_as_bookmarked(bookmarks[i], true);
  1122. }
  1123. }
  1124. if (previous_state.is_empty()) {
  1125. previous_state = p_state;
  1126. }
  1127. }
  1128. Variant CodeTextEditor::get_navigation_state() {
  1129. Dictionary state;
  1130. state["scroll_position"] = text_editor->get_v_scroll();
  1131. state["h_scroll_position"] = text_editor->get_h_scroll();
  1132. state["column"] = text_editor->get_caret_column();
  1133. state["row"] = text_editor->get_caret_line();
  1134. state["selection"] = get_text_editor()->has_selection();
  1135. if (get_text_editor()->has_selection()) {
  1136. state["selection_from_line"] = text_editor->get_selection_from_line();
  1137. state["selection_from_column"] = text_editor->get_selection_from_column();
  1138. state["selection_to_line"] = text_editor->get_selection_to_line();
  1139. state["selection_to_column"] = text_editor->get_selection_to_column();
  1140. }
  1141. return state;
  1142. }
  1143. void CodeTextEditor::set_error(const String &p_error) {
  1144. error->set_text(p_error);
  1145. if (!p_error.is_empty()) {
  1146. error->set_default_cursor_shape(CURSOR_POINTING_HAND);
  1147. } else {
  1148. error->set_default_cursor_shape(CURSOR_ARROW);
  1149. }
  1150. }
  1151. void CodeTextEditor::set_error_pos(int p_line, int p_column) {
  1152. error_line = p_line;
  1153. error_column = p_column;
  1154. }
  1155. Point2i CodeTextEditor::get_error_pos() const {
  1156. return Point2i(error_line, error_column);
  1157. }
  1158. void CodeTextEditor::goto_error() {
  1159. if (!error->get_text().is_empty()) {
  1160. if (text_editor->get_line_count() != error_line) {
  1161. text_editor->unfold_line(error_line);
  1162. }
  1163. text_editor->remove_secondary_carets();
  1164. text_editor->set_caret_line(error_line);
  1165. text_editor->set_caret_column(error_column);
  1166. text_editor->center_viewport_to_caret();
  1167. }
  1168. }
  1169. void CodeTextEditor::_update_text_editor_theme() {
  1170. emit_signal(SNAME("load_theme_settings"));
  1171. error_button->set_icon(get_editor_theme_icon(SNAME("StatusError")));
  1172. warning_button->set_icon(get_editor_theme_icon(SNAME("NodeWarning")));
  1173. Ref<Font> status_bar_font = get_theme_font(SNAME("status_source"), EditorStringName(EditorFonts));
  1174. int status_bar_font_size = get_theme_font_size(SNAME("status_source_size"), EditorStringName(EditorFonts));
  1175. int count = status_bar->get_child_count();
  1176. for (int i = 0; i < count; i++) {
  1177. Control *n = Object::cast_to<Control>(status_bar->get_child(i));
  1178. if (n) {
  1179. n->add_theme_font_override(SNAME("font"), status_bar_font);
  1180. n->add_theme_font_size_override(SNAME("font_size"), status_bar_font_size);
  1181. }
  1182. }
  1183. const Color &error_color = get_theme_color(SNAME("error_color"), EditorStringName(Editor));
  1184. const Color &warning_color = get_theme_color(SNAME("warning_color"), EditorStringName(Editor));
  1185. error->add_theme_color_override(SNAME("font_color"), error_color);
  1186. error_button->add_theme_color_override(SNAME("font_color"), error_color);
  1187. warning_button->add_theme_color_override(SNAME("font_color"), warning_color);
  1188. _update_font_ligatures();
  1189. }
  1190. void CodeTextEditor::_update_font_ligatures() {
  1191. int ot_mode = EDITOR_GET("interface/editor/code_font_contextual_ligatures");
  1192. Ref<FontVariation> fc = text_editor->get_theme_font(SNAME("font"));
  1193. if (fc.is_valid()) {
  1194. switch (ot_mode) {
  1195. case 1: { // Disable ligatures.
  1196. Dictionary ftrs;
  1197. ftrs[TS->name_to_tag("calt")] = 0;
  1198. fc->set_opentype_features(ftrs);
  1199. } break;
  1200. case 2: { // Custom.
  1201. Vector<String> subtag = String(EDITOR_GET("interface/editor/code_font_custom_opentype_features")).split(",");
  1202. Dictionary ftrs;
  1203. for (int i = 0; i < subtag.size(); i++) {
  1204. Vector<String> subtag_a = subtag[i].split("=");
  1205. if (subtag_a.size() == 2) {
  1206. ftrs[TS->name_to_tag(subtag_a[0])] = subtag_a[1].to_int();
  1207. } else if (subtag_a.size() == 1) {
  1208. ftrs[TS->name_to_tag(subtag_a[0])] = 1;
  1209. }
  1210. }
  1211. fc->set_opentype_features(ftrs);
  1212. } break;
  1213. default: { // Enabled.
  1214. Dictionary ftrs;
  1215. ftrs[TS->name_to_tag("calt")] = 1;
  1216. fc->set_opentype_features(ftrs);
  1217. } break;
  1218. }
  1219. }
  1220. }
  1221. void CodeTextEditor::_text_changed_idle_timeout() {
  1222. _validate_script();
  1223. emit_signal(SNAME("validate_script"));
  1224. }
  1225. void CodeTextEditor::validate_script() {
  1226. idle->start();
  1227. }
  1228. void CodeTextEditor::_error_button_pressed() {
  1229. _set_show_errors_panel(!is_errors_panel_opened);
  1230. _set_show_warnings_panel(false);
  1231. }
  1232. void CodeTextEditor::_warning_button_pressed() {
  1233. _set_show_warnings_panel(!is_warnings_panel_opened);
  1234. _set_show_errors_panel(false);
  1235. }
  1236. void CodeTextEditor::_zoom_popup_id_pressed(int p_idx) {
  1237. _zoom_to(zoom_button->get_popup()->get_item_metadata(p_idx));
  1238. }
  1239. void CodeTextEditor::_set_show_errors_panel(bool p_show) {
  1240. is_errors_panel_opened = p_show;
  1241. emit_signal(SNAME("show_errors_panel"), p_show);
  1242. }
  1243. void CodeTextEditor::_set_show_warnings_panel(bool p_show) {
  1244. is_warnings_panel_opened = p_show;
  1245. emit_signal(SNAME("show_warnings_panel"), p_show);
  1246. }
  1247. void CodeTextEditor::_toggle_scripts_pressed() {
  1248. ScriptEditor::get_singleton()->toggle_scripts_panel();
  1249. update_toggle_scripts_button();
  1250. }
  1251. void CodeTextEditor::_error_pressed(const Ref<InputEvent> &p_event) {
  1252. Ref<InputEventMouseButton> mb = p_event;
  1253. if (mb.is_valid() && mb->is_pressed() && mb->get_button_index() == MouseButton::LEFT) {
  1254. goto_error();
  1255. }
  1256. }
  1257. void CodeTextEditor::_notification(int p_what) {
  1258. switch (p_what) {
  1259. case NOTIFICATION_THEME_CHANGED: {
  1260. if (toggle_scripts_button->is_visible()) {
  1261. update_toggle_scripts_button();
  1262. }
  1263. _update_text_editor_theme();
  1264. } break;
  1265. case NOTIFICATION_VISIBILITY_CHANGED: {
  1266. if (toggle_scripts_button->is_visible()) {
  1267. update_toggle_scripts_button();
  1268. }
  1269. set_process_input(is_visible_in_tree());
  1270. } break;
  1271. case NOTIFICATION_PREDELETE: {
  1272. if (find_replace_bar) {
  1273. find_replace_bar->set_text_edit(nullptr);
  1274. }
  1275. } break;
  1276. }
  1277. }
  1278. void CodeTextEditor::set_error_count(int p_error_count) {
  1279. error_button->set_text(itos(p_error_count));
  1280. error_button->set_visible(p_error_count > 0);
  1281. if (!p_error_count) {
  1282. _set_show_errors_panel(false);
  1283. }
  1284. }
  1285. void CodeTextEditor::set_warning_count(int p_warning_count) {
  1286. warning_button->set_text(itos(p_warning_count));
  1287. warning_button->set_visible(p_warning_count > 0);
  1288. if (!p_warning_count) {
  1289. _set_show_warnings_panel(false);
  1290. }
  1291. }
  1292. void CodeTextEditor::toggle_bookmark() {
  1293. Vector<int> sorted_carets = text_editor->get_sorted_carets();
  1294. int last_line = -1;
  1295. for (const int &c : sorted_carets) {
  1296. int from = text_editor->get_selection_from_line(c);
  1297. from += from == last_line ? 1 : 0;
  1298. int to = text_editor->get_selection_to_line(c);
  1299. if (to < from) {
  1300. continue;
  1301. }
  1302. // Check first if there's any bookmarked lines in the selection.
  1303. bool selection_has_bookmarks = false;
  1304. for (int line = from; line <= to; line++) {
  1305. if (text_editor->is_line_bookmarked(line)) {
  1306. selection_has_bookmarks = true;
  1307. break;
  1308. }
  1309. }
  1310. // Set bookmark on caret or remove all bookmarks from the selection.
  1311. if (!selection_has_bookmarks) {
  1312. if (text_editor->get_caret_line(c) != last_line) {
  1313. text_editor->set_line_as_bookmarked(text_editor->get_caret_line(c), true);
  1314. }
  1315. } else {
  1316. for (int line = from; line <= to; line++) {
  1317. text_editor->set_line_as_bookmarked(line, false);
  1318. }
  1319. }
  1320. last_line = to;
  1321. }
  1322. }
  1323. void CodeTextEditor::goto_next_bookmark() {
  1324. PackedInt32Array bmarks = text_editor->get_bookmarked_lines();
  1325. if (bmarks.size() <= 0) {
  1326. return;
  1327. }
  1328. int current_line = text_editor->get_caret_line();
  1329. int bmark_idx = 0;
  1330. if (current_line < (int)bmarks[bmarks.size() - 1]) {
  1331. while (bmark_idx < bmarks.size() && bmarks[bmark_idx] <= current_line) {
  1332. bmark_idx++;
  1333. }
  1334. }
  1335. goto_line_centered(bmarks[bmark_idx]);
  1336. }
  1337. void CodeTextEditor::goto_prev_bookmark() {
  1338. PackedInt32Array bmarks = text_editor->get_bookmarked_lines();
  1339. if (bmarks.size() <= 0) {
  1340. return;
  1341. }
  1342. int current_line = text_editor->get_caret_line();
  1343. int bmark_idx = bmarks.size() - 1;
  1344. if (current_line > (int)bmarks[0]) {
  1345. while (bmark_idx >= 0 && bmarks[bmark_idx] >= current_line) {
  1346. bmark_idx--;
  1347. }
  1348. }
  1349. goto_line_centered(bmarks[bmark_idx]);
  1350. }
  1351. void CodeTextEditor::remove_all_bookmarks() {
  1352. text_editor->clear_bookmarked_lines();
  1353. }
  1354. void CodeTextEditor::_zoom_in() {
  1355. int s = text_editor->get_theme_font_size("font_size");
  1356. _zoom_to(zoom_factor * (s + MAX(1.0f, EDSCALE)) / s);
  1357. }
  1358. void CodeTextEditor::_zoom_out() {
  1359. int s = text_editor->get_theme_font_size("font_size");
  1360. _zoom_to(zoom_factor * (s - MAX(1.0f, EDSCALE)) / s);
  1361. }
  1362. void CodeTextEditor::_zoom_to(float p_zoom_factor) {
  1363. if (zoom_factor == p_zoom_factor) {
  1364. return;
  1365. }
  1366. float old_zoom_factor = zoom_factor;
  1367. set_zoom_factor(p_zoom_factor);
  1368. if (old_zoom_factor != zoom_factor) {
  1369. emit_signal(SNAME("zoomed"), zoom_factor);
  1370. }
  1371. }
  1372. void CodeTextEditor::set_zoom_factor(float p_zoom_factor) {
  1373. int preset_count = sizeof(ZOOM_FACTOR_PRESETS) / sizeof(float);
  1374. zoom_factor = CLAMP(p_zoom_factor, ZOOM_FACTOR_PRESETS[0], ZOOM_FACTOR_PRESETS[preset_count - 1]);
  1375. int neutral_font_size = int(EDITOR_GET("interface/editor/code_font_size")) * EDSCALE;
  1376. int new_font_size = Math::round(zoom_factor * neutral_font_size);
  1377. zoom_button->set_text(itos(Math::round(zoom_factor * 100)) + " %");
  1378. if (text_editor->has_theme_font_size_override("font_size")) {
  1379. text_editor->remove_theme_font_size_override("font_size");
  1380. }
  1381. text_editor->add_theme_font_size_override("font_size", new_font_size);
  1382. }
  1383. float CodeTextEditor::get_zoom_factor() {
  1384. return zoom_factor;
  1385. }
  1386. void CodeTextEditor::_bind_methods() {
  1387. ADD_SIGNAL(MethodInfo("validate_script"));
  1388. ADD_SIGNAL(MethodInfo("load_theme_settings"));
  1389. ADD_SIGNAL(MethodInfo("show_errors_panel"));
  1390. ADD_SIGNAL(MethodInfo("show_warnings_panel"));
  1391. ADD_SIGNAL(MethodInfo("zoomed", PropertyInfo(Variant::FLOAT, "p_zoom_factor")));
  1392. }
  1393. void CodeTextEditor::set_code_complete_func(CodeTextEditorCodeCompleteFunc p_code_complete_func, void *p_ud) {
  1394. code_complete_func = p_code_complete_func;
  1395. code_complete_ud = p_ud;
  1396. }
  1397. void CodeTextEditor::show_toggle_scripts_button() {
  1398. toggle_scripts_button->show();
  1399. }
  1400. void CodeTextEditor::update_toggle_scripts_button() {
  1401. if (is_layout_rtl()) {
  1402. toggle_scripts_button->set_icon(get_editor_theme_icon(ScriptEditor::get_singleton()->is_scripts_panel_toggled() ? SNAME("Forward") : SNAME("Back")));
  1403. } else {
  1404. toggle_scripts_button->set_icon(get_editor_theme_icon(ScriptEditor::get_singleton()->is_scripts_panel_toggled() ? SNAME("Back") : SNAME("Forward")));
  1405. }
  1406. toggle_scripts_button->set_tooltip_text(vformat("%s (%s)", TTR("Toggle Scripts Panel"), ED_GET_SHORTCUT("script_editor/toggle_scripts_panel")->get_as_text()));
  1407. }
  1408. CodeTextEditor::CodeTextEditor() {
  1409. code_complete_func = nullptr;
  1410. ED_SHORTCUT("script_editor/zoom_in", TTR("Zoom In"), KeyModifierMask::CMD_OR_CTRL | Key::EQUAL);
  1411. ED_SHORTCUT("script_editor/zoom_out", TTR("Zoom Out"), KeyModifierMask::CMD_OR_CTRL | Key::MINUS);
  1412. ED_SHORTCUT_ARRAY("script_editor/reset_zoom", TTR("Reset Zoom"),
  1413. { int32_t(KeyModifierMask::CMD_OR_CTRL | Key::KEY_0), int32_t(KeyModifierMask::CMD_OR_CTRL | Key::KP_0) });
  1414. text_editor = memnew(CodeEdit);
  1415. add_child(text_editor);
  1416. text_editor->set_v_size_flags(SIZE_EXPAND_FILL);
  1417. text_editor->set_structured_text_bidi_override(TextServer::STRUCTURED_TEXT_GDSCRIPT);
  1418. text_editor->set_draw_bookmarks_gutter(true);
  1419. text_editor->set_draw_line_numbers(true);
  1420. text_editor->set_highlight_matching_braces_enabled(true);
  1421. text_editor->set_auto_indent_enabled(true);
  1422. text_editor->set_deselect_on_focus_loss_enabled(false);
  1423. status_bar = memnew(HBoxContainer);
  1424. add_child(status_bar);
  1425. status_bar->set_h_size_flags(SIZE_EXPAND_FILL);
  1426. status_bar->set_custom_minimum_size(Size2(0, 24 * EDSCALE)); // Adjust for the height of the warning icon.
  1427. idle = memnew(Timer);
  1428. add_child(idle);
  1429. idle->set_one_shot(true);
  1430. code_complete_enabled = EDITOR_GET("text_editor/completion/code_complete_enabled");
  1431. code_complete_timer = memnew(Timer);
  1432. add_child(code_complete_timer);
  1433. code_complete_timer->set_one_shot(true);
  1434. error_line = 0;
  1435. error_column = 0;
  1436. toggle_scripts_button = memnew(Button);
  1437. toggle_scripts_button->set_flat(true);
  1438. toggle_scripts_button->set_v_size_flags(SIZE_EXPAND | SIZE_SHRINK_CENTER);
  1439. toggle_scripts_button->connect(SceneStringName(pressed), callable_mp(this, &CodeTextEditor::_toggle_scripts_pressed));
  1440. status_bar->add_child(toggle_scripts_button);
  1441. toggle_scripts_button->hide();
  1442. // Error
  1443. ScrollContainer *scroll = memnew(ScrollContainer);
  1444. scroll->set_h_size_flags(SIZE_EXPAND_FILL);
  1445. scroll->set_v_size_flags(SIZE_EXPAND_FILL);
  1446. scroll->set_vertical_scroll_mode(ScrollContainer::SCROLL_MODE_DISABLED);
  1447. status_bar->add_child(scroll);
  1448. error = memnew(Label);
  1449. scroll->add_child(error);
  1450. error->set_v_size_flags(SIZE_EXPAND | SIZE_SHRINK_CENTER);
  1451. error->set_mouse_filter(MOUSE_FILTER_STOP);
  1452. error->connect(SceneStringName(gui_input), callable_mp(this, &CodeTextEditor::_error_pressed));
  1453. // Errors
  1454. error_button = memnew(Button);
  1455. error_button->set_flat(true);
  1456. status_bar->add_child(error_button);
  1457. error_button->set_v_size_flags(SIZE_EXPAND | SIZE_SHRINK_CENTER);
  1458. error_button->set_default_cursor_shape(CURSOR_POINTING_HAND);
  1459. error_button->connect(SceneStringName(pressed), callable_mp(this, &CodeTextEditor::_error_button_pressed));
  1460. error_button->set_tooltip_text(TTR("Errors"));
  1461. set_error_count(0);
  1462. // Warnings
  1463. warning_button = memnew(Button);
  1464. warning_button->set_flat(true);
  1465. status_bar->add_child(warning_button);
  1466. warning_button->set_v_size_flags(SIZE_EXPAND | SIZE_SHRINK_CENTER);
  1467. warning_button->set_default_cursor_shape(CURSOR_POINTING_HAND);
  1468. warning_button->connect(SceneStringName(pressed), callable_mp(this, &CodeTextEditor::_warning_button_pressed));
  1469. warning_button->set_tooltip_text(TTR("Warnings"));
  1470. set_warning_count(0);
  1471. status_bar->add_child(memnew(VSeparator));
  1472. // Zoom
  1473. zoom_button = memnew(MenuButton);
  1474. status_bar->add_child(zoom_button);
  1475. zoom_button->set_flat(true);
  1476. zoom_button->set_v_size_flags(SIZE_EXPAND | SIZE_SHRINK_CENTER);
  1477. zoom_button->set_tooltip_text(TTR("Zoom factor"));
  1478. zoom_button->set_text("100 %");
  1479. PopupMenu *zoom_menu = zoom_button->get_popup();
  1480. int preset_count = sizeof(ZOOM_FACTOR_PRESETS) / sizeof(float);
  1481. for (int i = 0; i < preset_count; i++) {
  1482. float z = ZOOM_FACTOR_PRESETS[i];
  1483. zoom_menu->add_item(itos(Math::round(z * 100)) + " %");
  1484. zoom_menu->set_item_metadata(i, z);
  1485. }
  1486. zoom_menu->connect("id_pressed", callable_mp(this, &CodeTextEditor::_zoom_popup_id_pressed));
  1487. status_bar->add_child(memnew(VSeparator));
  1488. // Line and column
  1489. line_and_col_txt = memnew(Label);
  1490. status_bar->add_child(line_and_col_txt);
  1491. line_and_col_txt->set_v_size_flags(SIZE_EXPAND | SIZE_SHRINK_CENTER);
  1492. line_and_col_txt->set_tooltip_text(TTR("Line and column numbers."));
  1493. line_and_col_txt->set_mouse_filter(MOUSE_FILTER_STOP);
  1494. status_bar->add_child(memnew(VSeparator));
  1495. // Indentation
  1496. indentation_txt = memnew(Label);
  1497. status_bar->add_child(indentation_txt);
  1498. indentation_txt->set_v_size_flags(SIZE_EXPAND | SIZE_SHRINK_CENTER);
  1499. indentation_txt->set_tooltip_text(TTR("Indentation"));
  1500. indentation_txt->set_mouse_filter(MOUSE_FILTER_STOP);
  1501. text_editor->connect(SceneStringName(gui_input), callable_mp(this, &CodeTextEditor::_text_editor_gui_input));
  1502. text_editor->connect("caret_changed", callable_mp(this, &CodeTextEditor::_line_col_changed));
  1503. text_editor->connect("text_changed", callable_mp(this, &CodeTextEditor::_text_changed));
  1504. text_editor->connect("code_completion_requested", callable_mp(this, &CodeTextEditor::_complete_request));
  1505. TypedArray<String> cs;
  1506. cs.push_back(".");
  1507. cs.push_back(",");
  1508. cs.push_back("(");
  1509. cs.push_back("=");
  1510. cs.push_back("$");
  1511. cs.push_back("@");
  1512. cs.push_back("\"");
  1513. cs.push_back("\'");
  1514. text_editor->set_code_completion_prefixes(cs);
  1515. idle->connect("timeout", callable_mp(this, &CodeTextEditor::_text_changed_idle_timeout));
  1516. code_complete_timer->connect("timeout", callable_mp(this, &CodeTextEditor::_code_complete_timer_timeout));
  1517. add_theme_constant_override("separation", 4 * EDSCALE);
  1518. }