code_editor.cpp 72 KB

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