code_editor.cpp 65 KB

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