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