code_editor.cpp 62 KB

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