code_editor.cpp 71 KB

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