code_editor.cpp 70 KB

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