code_editor.cpp 71 KB

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