2
0

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