code_editor.cpp 70 KB

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