code_editor.cpp 66 KB

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