code_editor.cpp 72 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212
  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. text_editor->add_theme_constant_override("line_spacing", EDITOR_GET("text_editor/appearance/whitespace/line_spacing"));
  873. // Behavior: Navigation
  874. text_editor->set_scroll_past_end_of_file_enabled(EDITOR_GET("text_editor/behavior/navigation/scroll_past_end_of_file"));
  875. text_editor->set_smooth_scroll_enabled(EDITOR_GET("text_editor/behavior/navigation/smooth_scrolling"));
  876. text_editor->set_v_scroll_speed(EDITOR_GET("text_editor/behavior/navigation/v_scroll_speed"));
  877. text_editor->set_drag_and_drop_selection_enabled(EDITOR_GET("text_editor/behavior/navigation/drag_and_drop_selection"));
  878. // Behavior: indent
  879. text_editor->set_indent_using_spaces(EDITOR_GET("text_editor/behavior/indent/type"));
  880. text_editor->set_indent_size(EDITOR_GET("text_editor/behavior/indent/size"));
  881. text_editor->set_auto_indent_enabled(EDITOR_GET("text_editor/behavior/indent/auto_indent"));
  882. // Completion
  883. text_editor->set_auto_brace_completion_enabled(EDITOR_GET("text_editor/completion/auto_brace_complete"));
  884. // Appearance: Guidelines
  885. if (EDITOR_GET("text_editor/appearance/guidelines/show_line_length_guidelines")) {
  886. TypedArray<int> guideline_cols;
  887. guideline_cols.append(EDITOR_GET("text_editor/appearance/guidelines/line_length_guideline_hard_column"));
  888. if (EDITOR_GET("text_editor/appearance/guidelines/line_length_guideline_soft_column") != guideline_cols[0]) {
  889. guideline_cols.append(EDITOR_GET("text_editor/appearance/guidelines/line_length_guideline_soft_column"));
  890. }
  891. text_editor->set_line_length_guidelines(guideline_cols);
  892. } else {
  893. text_editor->set_line_length_guidelines(TypedArray<int>());
  894. }
  895. }
  896. void CodeTextEditor::set_find_replace_bar(FindReplaceBar *p_bar) {
  897. if (find_replace_bar) {
  898. return;
  899. }
  900. find_replace_bar = p_bar;
  901. find_replace_bar->set_text_edit(this);
  902. find_replace_bar->connect("error", callable_mp(error, &Label::set_text));
  903. }
  904. void CodeTextEditor::remove_find_replace_bar() {
  905. if (!find_replace_bar) {
  906. return;
  907. }
  908. find_replace_bar->disconnect("error", callable_mp(error, &Label::set_text));
  909. find_replace_bar = nullptr;
  910. }
  911. void CodeTextEditor::trim_trailing_whitespace() {
  912. bool trimmed_whitespace = false;
  913. for (int i = 0; i < text_editor->get_line_count(); i++) {
  914. String line = text_editor->get_line(i);
  915. if (line.ends_with(" ") || line.ends_with("\t")) {
  916. if (!trimmed_whitespace) {
  917. text_editor->begin_complex_operation();
  918. trimmed_whitespace = true;
  919. }
  920. int end = 0;
  921. for (int j = line.length() - 1; j > -1; j--) {
  922. if (line[j] != ' ' && line[j] != '\t') {
  923. end = j + 1;
  924. break;
  925. }
  926. }
  927. text_editor->set_line(i, line.substr(0, end));
  928. }
  929. }
  930. if (trimmed_whitespace) {
  931. text_editor->merge_overlapping_carets();
  932. text_editor->end_complex_operation();
  933. text_editor->queue_redraw();
  934. }
  935. }
  936. void CodeTextEditor::insert_final_newline() {
  937. int final_line = text_editor->get_line_count() - 1;
  938. String line = text_editor->get_line(final_line);
  939. // Length 0 means it's already an empty line, no need to add a newline.
  940. if (line.length() > 0 && !line.ends_with("\n")) {
  941. text_editor->begin_complex_operation();
  942. line += "\n";
  943. text_editor->set_line(final_line, line);
  944. text_editor->end_complex_operation();
  945. text_editor->queue_redraw();
  946. }
  947. }
  948. void CodeTextEditor::convert_indent_to_spaces() {
  949. int indent_size = EDITOR_GET("text_editor/behavior/indent/size");
  950. String indent = "";
  951. for (int i = 0; i < indent_size; i++) {
  952. indent += " ";
  953. }
  954. Vector<int> cursor_columns;
  955. cursor_columns.resize(text_editor->get_caret_count());
  956. for (int c = 0; c < text_editor->get_caret_count(); c++) {
  957. cursor_columns.write[c] = text_editor->get_caret_column(c);
  958. }
  959. bool changed_indentation = false;
  960. for (int i = 0; i < text_editor->get_line_count(); i++) {
  961. String line = text_editor->get_line(i);
  962. if (line.length() <= 0) {
  963. continue;
  964. }
  965. int j = 0;
  966. while (j < line.length() && (line[j] == ' ' || line[j] == '\t')) {
  967. if (line[j] == '\t') {
  968. if (!changed_indentation) {
  969. text_editor->begin_complex_operation();
  970. changed_indentation = true;
  971. }
  972. for (int c = 0; c < text_editor->get_caret_count(); c++) {
  973. if (text_editor->get_caret_line(c) == i && text_editor->get_caret_column(c) > j) {
  974. cursor_columns.write[c] += indent_size - 1;
  975. }
  976. }
  977. line = line.left(j) + indent + line.substr(j + 1);
  978. }
  979. j++;
  980. }
  981. if (changed_indentation) {
  982. text_editor->set_line(i, line);
  983. }
  984. }
  985. if (changed_indentation) {
  986. for (int c = 0; c < text_editor->get_caret_count(); c++) {
  987. text_editor->set_caret_column(cursor_columns[c], c == 0, c);
  988. }
  989. text_editor->merge_overlapping_carets();
  990. text_editor->end_complex_operation();
  991. text_editor->queue_redraw();
  992. }
  993. }
  994. void CodeTextEditor::convert_indent_to_tabs() {
  995. int indent_size = EDITOR_GET("text_editor/behavior/indent/size");
  996. indent_size -= 1;
  997. Vector<int> cursor_columns;
  998. cursor_columns.resize(text_editor->get_caret_count());
  999. for (int c = 0; c < text_editor->get_caret_count(); c++) {
  1000. cursor_columns.write[c] = text_editor->get_caret_column(c);
  1001. }
  1002. bool changed_indentation = false;
  1003. for (int i = 0; i < text_editor->get_line_count(); i++) {
  1004. String line = text_editor->get_line(i);
  1005. if (line.length() <= 0) {
  1006. continue;
  1007. }
  1008. int j = 0;
  1009. int space_count = -1;
  1010. while (j < line.length() && (line[j] == ' ' || line[j] == '\t')) {
  1011. if (line[j] != '\t') {
  1012. space_count++;
  1013. if (space_count == indent_size) {
  1014. if (!changed_indentation) {
  1015. text_editor->begin_complex_operation();
  1016. changed_indentation = true;
  1017. }
  1018. for (int c = 0; c < text_editor->get_caret_count(); c++) {
  1019. if (text_editor->get_caret_line(c) == i && text_editor->get_caret_column(c) > j) {
  1020. cursor_columns.write[c] -= indent_size;
  1021. }
  1022. }
  1023. line = line.left(j - indent_size) + "\t" + line.substr(j + 1);
  1024. j = 0;
  1025. space_count = -1;
  1026. }
  1027. } else {
  1028. space_count = -1;
  1029. }
  1030. j++;
  1031. }
  1032. if (changed_indentation) {
  1033. text_editor->set_line(i, line);
  1034. }
  1035. }
  1036. if (changed_indentation) {
  1037. for (int c = 0; c < text_editor->get_caret_count(); c++) {
  1038. text_editor->set_caret_column(cursor_columns[c], c == 0, c);
  1039. }
  1040. text_editor->merge_overlapping_carets();
  1041. text_editor->end_complex_operation();
  1042. text_editor->queue_redraw();
  1043. }
  1044. }
  1045. void CodeTextEditor::convert_case(CaseStyle p_case) {
  1046. if (!text_editor->has_selection()) {
  1047. return;
  1048. }
  1049. text_editor->begin_complex_operation();
  1050. Vector<int> caret_edit_order = text_editor->get_caret_index_edit_order();
  1051. for (const int &c : caret_edit_order) {
  1052. if (!text_editor->has_selection(c)) {
  1053. continue;
  1054. }
  1055. int begin = text_editor->get_selection_from_line(c);
  1056. int end = text_editor->get_selection_to_line(c);
  1057. int begin_col = text_editor->get_selection_from_column(c);
  1058. int end_col = text_editor->get_selection_to_column(c);
  1059. for (int i = begin; i <= end; i++) {
  1060. int len = text_editor->get_line(i).length();
  1061. if (i == end) {
  1062. len = end_col;
  1063. }
  1064. if (i == begin) {
  1065. len -= begin_col;
  1066. }
  1067. String new_line = text_editor->get_line(i).substr(i == begin ? begin_col : 0, len);
  1068. switch (p_case) {
  1069. case UPPER: {
  1070. new_line = new_line.to_upper();
  1071. } break;
  1072. case LOWER: {
  1073. new_line = new_line.to_lower();
  1074. } break;
  1075. case CAPITALIZE: {
  1076. new_line = new_line.capitalize();
  1077. } break;
  1078. }
  1079. if (i == begin) {
  1080. new_line = text_editor->get_line(i).left(begin_col) + new_line;
  1081. }
  1082. if (i == end) {
  1083. new_line = new_line + text_editor->get_line(i).substr(end_col);
  1084. }
  1085. text_editor->set_line(i, new_line);
  1086. }
  1087. }
  1088. text_editor->end_complex_operation();
  1089. }
  1090. void CodeTextEditor::move_lines_up() {
  1091. text_editor->begin_complex_operation();
  1092. Vector<int> caret_edit_order = text_editor->get_caret_index_edit_order();
  1093. // Lists of carets representing each group.
  1094. Vector<Vector<int>> caret_groups;
  1095. Vector<Pair<int, int>> group_borders;
  1096. // Search for groups of carets and their selections residing on the same lines.
  1097. for (int i = 0; i < caret_edit_order.size(); i++) {
  1098. int c = caret_edit_order[i];
  1099. Vector<int> new_group{ c };
  1100. Pair<int, int> group_border;
  1101. group_border.first = _get_affected_lines_from(c);
  1102. group_border.second = _get_affected_lines_to(c);
  1103. for (int j = i; j < caret_edit_order.size() - 1; j++) {
  1104. int c_current = caret_edit_order[j];
  1105. int c_next = caret_edit_order[j + 1];
  1106. int next_start_pos = _get_affected_lines_from(c_next);
  1107. int next_end_pos = _get_affected_lines_to(c_next);
  1108. 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);
  1109. i = j;
  1110. if (next_end_pos != current_start_pos && next_end_pos + 1 != current_start_pos) {
  1111. break;
  1112. }
  1113. group_border.first = next_start_pos;
  1114. new_group.push_back(c_next);
  1115. // If the last caret is added to the current group there is no need to process it again.
  1116. if (j + 1 == caret_edit_order.size() - 1) {
  1117. i++;
  1118. }
  1119. }
  1120. group_borders.push_back(group_border);
  1121. caret_groups.push_back(new_group);
  1122. }
  1123. for (int i = group_borders.size() - 1; i >= 0; i--) {
  1124. if (group_borders[i].first - 1 < 0) {
  1125. continue;
  1126. }
  1127. // If the group starts overlapping with the upper group don't move it.
  1128. if (i < group_borders.size() - 1 && group_borders[i].first - 1 <= group_borders[i + 1].second) {
  1129. continue;
  1130. }
  1131. // We have to remember caret positions and selections prior to line swapping.
  1132. Vector<Vector<int>> caret_group_parameters;
  1133. for (int j = 0; j < caret_groups[i].size(); j++) {
  1134. int c = caret_groups[i][j];
  1135. int cursor_line = text_editor->get_caret_line(c);
  1136. int cursor_column = text_editor->get_caret_column(c);
  1137. if (!text_editor->has_selection(c)) {
  1138. caret_group_parameters.push_back(Vector<int>{ -1, -1, -1, -1, cursor_line, cursor_column });
  1139. continue;
  1140. }
  1141. int from_line = text_editor->get_selection_from_line(c);
  1142. int from_col = text_editor->get_selection_from_column(c);
  1143. int to_line = text_editor->get_selection_to_line(c);
  1144. int to_column = text_editor->get_selection_to_column(c);
  1145. caret_group_parameters.push_back(Vector<int>{ from_line, from_col, to_line, to_column, cursor_line, cursor_column });
  1146. }
  1147. for (int line_id = group_borders[i].first; line_id <= group_borders[i].second; line_id++) {
  1148. text_editor->unfold_line(line_id);
  1149. text_editor->unfold_line(line_id - 1);
  1150. text_editor->swap_lines(line_id - 1, line_id);
  1151. }
  1152. for (int j = 0; j < caret_groups[i].size(); j++) {
  1153. int c = caret_groups[i][j];
  1154. Vector<int> caret_parameters = caret_group_parameters[j];
  1155. text_editor->set_caret_line(caret_parameters[4] - 1, c == 0, true, 0, c);
  1156. text_editor->set_caret_column(caret_parameters[5], c == 0, c);
  1157. if (caret_parameters[0] >= 0) {
  1158. text_editor->select(caret_parameters[0] - 1, caret_parameters[1], caret_parameters[2] - 1, caret_parameters[3], c);
  1159. }
  1160. }
  1161. }
  1162. text_editor->end_complex_operation();
  1163. text_editor->merge_overlapping_carets();
  1164. text_editor->queue_redraw();
  1165. }
  1166. void CodeTextEditor::move_lines_down() {
  1167. text_editor->begin_complex_operation();
  1168. Vector<int> caret_edit_order = text_editor->get_caret_index_edit_order();
  1169. // Lists of carets representing each group.
  1170. Vector<Vector<int>> caret_groups;
  1171. Vector<Pair<int, int>> group_borders;
  1172. Vector<int> group_border_ends;
  1173. // Search for groups of carets and their selections residing on the same lines.
  1174. for (int i = 0; i < caret_edit_order.size(); i++) {
  1175. int c = caret_edit_order[i];
  1176. Vector<int> new_group{ c };
  1177. Pair<int, int> group_border;
  1178. group_border.first = _get_affected_lines_from(c);
  1179. group_border.second = _get_affected_lines_to(c);
  1180. for (int j = i; j < caret_edit_order.size() - 1; j++) {
  1181. int c_current = caret_edit_order[j];
  1182. int c_next = caret_edit_order[j + 1];
  1183. int next_start_pos = _get_affected_lines_from(c_next);
  1184. int next_end_pos = _get_affected_lines_to(c_next);
  1185. int current_start_pos = text_editor->has_selection(c_current) ? text_editor->get_selection_from_line(c_current) : text_editor->get_caret_line(c_current);
  1186. i = j;
  1187. if (next_end_pos == current_start_pos || next_end_pos + 1 == current_start_pos) {
  1188. group_border.first = next_start_pos;
  1189. new_group.push_back(c_next);
  1190. // If the last caret is added to the current group there is no need to process it again.
  1191. if (j + 1 == caret_edit_order.size() - 1) {
  1192. i++;
  1193. }
  1194. } else {
  1195. break;
  1196. }
  1197. }
  1198. group_borders.push_back(group_border);
  1199. group_border_ends.push_back(text_editor->has_selection(c) ? text_editor->get_selection_to_line(c) : text_editor->get_caret_line(c));
  1200. caret_groups.push_back(new_group);
  1201. }
  1202. for (int i = 0; i < group_borders.size(); i++) {
  1203. if (group_border_ends[i] + 1 > text_editor->get_line_count() - 1) {
  1204. continue;
  1205. }
  1206. // If the group starts overlapping with the upper group don't move it.
  1207. if (i > 0 && group_border_ends[i] + 1 >= group_borders[i - 1].first) {
  1208. continue;
  1209. }
  1210. // We have to remember caret positions and selections prior to line swapping.
  1211. Vector<Vector<int>> caret_group_parameters;
  1212. for (int j = 0; j < caret_groups[i].size(); j++) {
  1213. int c = caret_groups[i][j];
  1214. int cursor_line = text_editor->get_caret_line(c);
  1215. int cursor_column = text_editor->get_caret_column(c);
  1216. if (!text_editor->has_selection(c)) {
  1217. caret_group_parameters.push_back(Vector<int>{ -1, -1, -1, -1, cursor_line, cursor_column });
  1218. continue;
  1219. }
  1220. int from_line = text_editor->get_selection_from_line(c);
  1221. int from_col = text_editor->get_selection_from_column(c);
  1222. int to_line = text_editor->get_selection_to_line(c);
  1223. int to_column = text_editor->get_selection_to_column(c);
  1224. caret_group_parameters.push_back(Vector<int>{ from_line, from_col, to_line, to_column, cursor_line, cursor_column });
  1225. }
  1226. for (int line_id = group_borders[i].second; line_id >= group_borders[i].first; line_id--) {
  1227. text_editor->unfold_line(line_id);
  1228. text_editor->unfold_line(line_id + 1);
  1229. text_editor->swap_lines(line_id + 1, line_id);
  1230. }
  1231. for (int j = 0; j < caret_groups[i].size(); j++) {
  1232. int c = caret_groups[i][j];
  1233. Vector<int> caret_parameters = caret_group_parameters[j];
  1234. text_editor->set_caret_line(caret_parameters[4] + 1, c == 0, true, 0, c);
  1235. text_editor->set_caret_column(caret_parameters[5], c == 0, c);
  1236. if (caret_parameters[0] >= 0) {
  1237. text_editor->select(caret_parameters[0] + 1, caret_parameters[1], caret_parameters[2] + 1, caret_parameters[3], c);
  1238. }
  1239. }
  1240. }
  1241. text_editor->merge_overlapping_carets();
  1242. text_editor->end_complex_operation();
  1243. text_editor->queue_redraw();
  1244. }
  1245. void CodeTextEditor::delete_lines() {
  1246. text_editor->begin_complex_operation();
  1247. Vector<int> caret_edit_order = text_editor->get_caret_index_edit_order();
  1248. Vector<int> lines;
  1249. int last_line = INT_MAX;
  1250. for (const int &c : caret_edit_order) {
  1251. for (int line = _get_affected_lines_to(c); line >= _get_affected_lines_from(c); line--) {
  1252. if (line >= last_line) {
  1253. continue;
  1254. }
  1255. last_line = line;
  1256. lines.append(line);
  1257. }
  1258. }
  1259. for (const int &line : lines) {
  1260. if (line != text_editor->get_line_count() - 1) {
  1261. text_editor->remove_text(line, 0, line + 1, 0);
  1262. } else {
  1263. text_editor->remove_text(line - 1, text_editor->get_line(line - 1).length(), line, text_editor->get_line(line).length());
  1264. }
  1265. // Readjust carets.
  1266. int new_line = MIN(line, text_editor->get_line_count() - 1);
  1267. text_editor->unfold_line(new_line);
  1268. for (const int &c : caret_edit_order) {
  1269. if (text_editor->get_caret_line(c) == line || (text_editor->get_caret_line(c) == line + 1 && text_editor->get_caret_column(c) == 0)) {
  1270. text_editor->deselect(c);
  1271. text_editor->set_caret_line(new_line, c == 0, true, 0, c);
  1272. continue;
  1273. }
  1274. if (text_editor->get_caret_line(c) > line) {
  1275. text_editor->set_caret_line(text_editor->get_caret_line(c) - 1, c == 0, true, 0, c);
  1276. continue;
  1277. }
  1278. break;
  1279. }
  1280. }
  1281. text_editor->merge_overlapping_carets();
  1282. text_editor->end_complex_operation();
  1283. }
  1284. void CodeTextEditor::duplicate_selection() {
  1285. text_editor->begin_complex_operation();
  1286. Vector<int> caret_edit_order = text_editor->get_caret_index_edit_order();
  1287. for (const int &c : caret_edit_order) {
  1288. const int cursor_column = text_editor->get_caret_column(c);
  1289. int from_line = text_editor->get_caret_line(c);
  1290. int to_line = text_editor->get_caret_line(c);
  1291. int from_column = 0;
  1292. int to_column = 0;
  1293. int cursor_new_line = to_line + 1;
  1294. int cursor_new_column = text_editor->get_caret_column(c);
  1295. String new_text = "\n" + text_editor->get_line(from_line);
  1296. bool selection_active = false;
  1297. text_editor->set_caret_column(text_editor->get_line(from_line).length(), c == 0, c);
  1298. if (text_editor->has_selection(c)) {
  1299. from_column = text_editor->get_selection_from_column(c);
  1300. to_column = text_editor->get_selection_to_column(c);
  1301. from_line = text_editor->get_selection_from_line(c);
  1302. to_line = text_editor->get_selection_to_line(c);
  1303. cursor_new_line = to_line + text_editor->get_caret_line(c) - from_line;
  1304. cursor_new_column = to_column == cursor_column ? 2 * to_column - from_column : to_column;
  1305. new_text = text_editor->get_selected_text(c);
  1306. selection_active = true;
  1307. text_editor->set_caret_line(to_line, c == 0, true, 0, c);
  1308. text_editor->set_caret_column(to_column, c == 0, c);
  1309. }
  1310. for (int i = from_line; i <= to_line; i++) {
  1311. text_editor->unfold_line(i);
  1312. }
  1313. text_editor->deselect(c);
  1314. text_editor->insert_text_at_caret(new_text, c);
  1315. text_editor->set_caret_line(cursor_new_line, c == 0, true, 0, c);
  1316. text_editor->set_caret_column(cursor_new_column, c == 0, c);
  1317. if (selection_active) {
  1318. text_editor->select(to_line, to_column, 2 * to_line - from_line, to_line == from_line ? 2 * to_column - from_column : to_column, c);
  1319. }
  1320. }
  1321. text_editor->merge_overlapping_carets();
  1322. text_editor->end_complex_operation();
  1323. text_editor->queue_redraw();
  1324. }
  1325. void CodeTextEditor::toggle_inline_comment(const String &delimiter) {
  1326. text_editor->begin_complex_operation();
  1327. Vector<int> caret_edit_order = text_editor->get_caret_index_edit_order();
  1328. caret_edit_order.reverse();
  1329. int last_line = -1;
  1330. int folded_to = 0;
  1331. for (const int &c1 : caret_edit_order) {
  1332. int from = _get_affected_lines_from(c1);
  1333. from += from == last_line ? 1 + folded_to : 0;
  1334. int to = _get_affected_lines_to(c1);
  1335. last_line = to;
  1336. // If last line is folded, extends to the end of the folded section
  1337. if (text_editor->is_line_folded(to)) {
  1338. folded_to = text_editor->get_next_visible_line_offset_from(to + 1, 1) - 1;
  1339. to += folded_to;
  1340. }
  1341. // Check first if there's any uncommented lines in selection.
  1342. bool is_commented = true;
  1343. for (int line = from; line <= to; line++) {
  1344. if (!text_editor->get_line(line).begins_with(delimiter)) {
  1345. is_commented = false;
  1346. break;
  1347. }
  1348. }
  1349. // Caret positions need to be saved since they could be moved at the eol.
  1350. Vector<int> caret_cols;
  1351. Vector<int> selection_to_cols;
  1352. for (const int &c2 : caret_edit_order) {
  1353. if (text_editor->get_caret_line(c2) >= from && text_editor->get_caret_line(c2) <= to) {
  1354. caret_cols.append(text_editor->get_caret_column(c2));
  1355. }
  1356. if (text_editor->has_selection(c2) && text_editor->get_selection_to_line(c2) >= from && text_editor->get_selection_to_line(c2) <= to) {
  1357. selection_to_cols.append(text_editor->get_selection_to_column(c2));
  1358. }
  1359. }
  1360. // Comment/uncomment.
  1361. for (int line = from; line <= to; line++) {
  1362. String line_text = text_editor->get_line(line);
  1363. if (line_text.strip_edges().is_empty()) {
  1364. text_editor->set_line(line, delimiter);
  1365. continue;
  1366. }
  1367. if (is_commented) {
  1368. text_editor->set_line(line, line_text.substr(delimiter.length(), line_text.length()));
  1369. continue;
  1370. }
  1371. text_editor->set_line(line, delimiter + line_text);
  1372. }
  1373. // Readjust carets and selections.
  1374. int caret_i = 0;
  1375. int selection_i = 0;
  1376. int offset = (is_commented ? -1 : 1) * delimiter.length();
  1377. for (const int &c2 : caret_edit_order) {
  1378. bool is_line_selection = text_editor->has_selection(c2) && text_editor->get_selection_from_line(c2) < text_editor->get_selection_to_line(c2);
  1379. if (text_editor->get_caret_line(c2) >= from && text_editor->get_caret_line(c2) <= to) {
  1380. int caret_col = caret_cols[caret_i++];
  1381. caret_col += (is_line_selection && caret_col == 0) ? 0 : offset;
  1382. text_editor->set_caret_column(caret_col, c2 == 0, c2);
  1383. }
  1384. if (text_editor->has_selection(c2) && text_editor->get_selection_to_line(c2) >= from && text_editor->get_selection_to_line(c2) <= to) {
  1385. int from_col = text_editor->get_selection_from_column(c2);
  1386. from_col += (is_line_selection && from_col == 0) ? 0 : offset;
  1387. int to_col = selection_to_cols[selection_i++];
  1388. to_col += (to_col == 0) ? 0 : offset;
  1389. text_editor->select(
  1390. text_editor->get_selection_from_line(c2), from_col,
  1391. text_editor->get_selection_to_line(c2), to_col, c2);
  1392. }
  1393. }
  1394. }
  1395. text_editor->merge_overlapping_carets();
  1396. text_editor->end_complex_operation();
  1397. text_editor->queue_redraw();
  1398. }
  1399. void CodeTextEditor::goto_line(int p_line) {
  1400. text_editor->remove_secondary_carets();
  1401. text_editor->deselect();
  1402. text_editor->unfold_line(p_line);
  1403. text_editor->call_deferred(SNAME("set_caret_line"), p_line);
  1404. }
  1405. void CodeTextEditor::goto_line_selection(int p_line, int p_begin, int p_end) {
  1406. text_editor->remove_secondary_carets();
  1407. text_editor->unfold_line(p_line);
  1408. text_editor->call_deferred(SNAME("set_caret_line"), p_line);
  1409. text_editor->call_deferred(SNAME("set_caret_column"), p_begin);
  1410. text_editor->select(p_line, p_begin, p_line, p_end);
  1411. }
  1412. void CodeTextEditor::goto_line_centered(int p_line) {
  1413. goto_line(p_line);
  1414. text_editor->call_deferred(SNAME("center_viewport_to_caret"));
  1415. }
  1416. void CodeTextEditor::set_executing_line(int p_line) {
  1417. text_editor->set_line_as_executing(p_line, true);
  1418. }
  1419. void CodeTextEditor::clear_executing_line() {
  1420. text_editor->clear_executing_lines();
  1421. }
  1422. Variant CodeTextEditor::get_edit_state() {
  1423. Dictionary state;
  1424. state.merge(get_navigation_state());
  1425. state["folded_lines"] = text_editor->get_folded_lines();
  1426. state["breakpoints"] = text_editor->get_breakpointed_lines();
  1427. state["bookmarks"] = text_editor->get_bookmarked_lines();
  1428. Ref<EditorSyntaxHighlighter> syntax_highlighter = text_editor->get_syntax_highlighter();
  1429. state["syntax_highlighter"] = syntax_highlighter->_get_name();
  1430. return state;
  1431. }
  1432. void CodeTextEditor::set_edit_state(const Variant &p_state) {
  1433. Dictionary state = p_state;
  1434. /* update the row first as it sets the column to 0 */
  1435. text_editor->set_caret_line(state["row"]);
  1436. text_editor->set_caret_column(state["column"]);
  1437. text_editor->set_v_scroll(state["scroll_position"]);
  1438. text_editor->set_h_scroll(state["h_scroll_position"]);
  1439. if (state.get("selection", false)) {
  1440. text_editor->select(state["selection_from_line"], state["selection_from_column"], state["selection_to_line"], state["selection_to_column"]);
  1441. } else {
  1442. text_editor->deselect();
  1443. }
  1444. if (state.has("folded_lines")) {
  1445. Vector<int> folded_lines = state["folded_lines"];
  1446. for (int i = 0; i < folded_lines.size(); i++) {
  1447. text_editor->fold_line(folded_lines[i]);
  1448. }
  1449. }
  1450. if (state.has("breakpoints")) {
  1451. Array breakpoints = state["breakpoints"];
  1452. for (int i = 0; i < breakpoints.size(); i++) {
  1453. text_editor->set_line_as_breakpoint(breakpoints[i], true);
  1454. }
  1455. }
  1456. if (state.has("bookmarks")) {
  1457. Array bookmarks = state["bookmarks"];
  1458. for (int i = 0; i < bookmarks.size(); i++) {
  1459. text_editor->set_line_as_bookmarked(bookmarks[i], true);
  1460. }
  1461. }
  1462. }
  1463. Variant CodeTextEditor::get_navigation_state() {
  1464. Dictionary state;
  1465. state["scroll_position"] = text_editor->get_v_scroll();
  1466. state["h_scroll_position"] = text_editor->get_h_scroll();
  1467. state["column"] = text_editor->get_caret_column();
  1468. state["row"] = text_editor->get_caret_line();
  1469. state["selection"] = get_text_editor()->has_selection();
  1470. if (get_text_editor()->has_selection()) {
  1471. state["selection_from_line"] = text_editor->get_selection_from_line();
  1472. state["selection_from_column"] = text_editor->get_selection_from_column();
  1473. state["selection_to_line"] = text_editor->get_selection_to_line();
  1474. state["selection_to_column"] = text_editor->get_selection_to_column();
  1475. }
  1476. return state;
  1477. }
  1478. void CodeTextEditor::set_error(const String &p_error) {
  1479. error->set_text(p_error);
  1480. if (!p_error.is_empty()) {
  1481. error->set_default_cursor_shape(CURSOR_POINTING_HAND);
  1482. } else {
  1483. error->set_default_cursor_shape(CURSOR_ARROW);
  1484. }
  1485. }
  1486. void CodeTextEditor::set_error_pos(int p_line, int p_column) {
  1487. error_line = p_line;
  1488. error_column = p_column;
  1489. }
  1490. Point2i CodeTextEditor::get_error_pos() const {
  1491. return Point2i(error_line, error_column);
  1492. }
  1493. void CodeTextEditor::goto_error() {
  1494. if (!error->get_text().is_empty()) {
  1495. if (text_editor->get_line_count() != error_line) {
  1496. text_editor->unfold_line(error_line);
  1497. }
  1498. text_editor->remove_secondary_carets();
  1499. text_editor->set_caret_line(error_line);
  1500. text_editor->set_caret_column(error_column);
  1501. text_editor->center_viewport_to_caret();
  1502. }
  1503. }
  1504. void CodeTextEditor::_update_text_editor_theme() {
  1505. emit_signal(SNAME("load_theme_settings"));
  1506. error->begin_bulk_theme_override();
  1507. error->add_theme_font_override(SNAME("font"), get_theme_font(SNAME("status_source"), SNAME("EditorFonts")));
  1508. error->add_theme_font_size_override(SNAME("font_size"), get_theme_font_size(SNAME("status_source_size"), SNAME("EditorFonts")));
  1509. error->add_theme_color_override(SNAME("font_color"), get_theme_color(SNAME("error_color"), SNAME("Editor")));
  1510. Ref<Font> status_bar_font = get_theme_font(SNAME("status_source"), SNAME("EditorFonts"));
  1511. int status_bar_font_size = get_theme_font_size(SNAME("status_source_size"), SNAME("EditorFonts"));
  1512. error->add_theme_font_override("font", status_bar_font);
  1513. error->add_theme_font_size_override("font_size", status_bar_font_size);
  1514. int count = status_bar->get_child_count();
  1515. for (int i = 0; i < count; i++) {
  1516. Control *n = Object::cast_to<Control>(status_bar->get_child(i));
  1517. if (n) {
  1518. n->add_theme_font_override("font", status_bar_font);
  1519. n->add_theme_font_size_override("font_size", status_bar_font_size);
  1520. }
  1521. }
  1522. error->end_bulk_theme_override();
  1523. }
  1524. void CodeTextEditor::_on_settings_change() {
  1525. _apply_settings_change();
  1526. }
  1527. void CodeTextEditor::_apply_settings_change() {
  1528. _update_text_editor_theme();
  1529. font_size = EDITOR_GET("interface/editor/code_font_size");
  1530. int ot_mode = EDITOR_GET("interface/editor/code_font_contextual_ligatures");
  1531. Ref<FontVariation> fc = text_editor->get_theme_font(SNAME("font"));
  1532. if (fc.is_valid()) {
  1533. switch (ot_mode) {
  1534. case 1: { // Disable ligatures.
  1535. Dictionary ftrs;
  1536. ftrs[TS->name_to_tag("calt")] = 0;
  1537. fc->set_opentype_features(ftrs);
  1538. } break;
  1539. case 2: { // Custom.
  1540. Vector<String> subtag = String(EDITOR_GET("interface/editor/code_font_custom_opentype_features")).split(",");
  1541. Dictionary ftrs;
  1542. for (int i = 0; i < subtag.size(); i++) {
  1543. Vector<String> subtag_a = subtag[i].split("=");
  1544. if (subtag_a.size() == 2) {
  1545. ftrs[TS->name_to_tag(subtag_a[0])] = subtag_a[1].to_int();
  1546. } else if (subtag_a.size() == 1) {
  1547. ftrs[TS->name_to_tag(subtag_a[0])] = 1;
  1548. }
  1549. }
  1550. fc->set_opentype_features(ftrs);
  1551. } break;
  1552. default: { // Enabled.
  1553. Dictionary ftrs;
  1554. ftrs[TS->name_to_tag("calt")] = 1;
  1555. fc->set_opentype_features(ftrs);
  1556. } break;
  1557. }
  1558. }
  1559. text_editor->set_code_hint_draw_below(EDITOR_GET("text_editor/completion/put_callhint_tooltip_below_current_line"));
  1560. code_complete_timer->set_wait_time(EDITOR_GET("text_editor/completion/code_complete_delay"));
  1561. idle->set_wait_time(EDITOR_GET("text_editor/completion/idle_parse_delay"));
  1562. }
  1563. void CodeTextEditor::_text_changed_idle_timeout() {
  1564. _validate_script();
  1565. emit_signal(SNAME("validate_script"));
  1566. }
  1567. void CodeTextEditor::validate_script() {
  1568. idle->start();
  1569. }
  1570. void CodeTextEditor::_error_button_pressed() {
  1571. _set_show_errors_panel(!is_errors_panel_opened);
  1572. _set_show_warnings_panel(false);
  1573. }
  1574. void CodeTextEditor::_warning_button_pressed() {
  1575. _set_show_warnings_panel(!is_warnings_panel_opened);
  1576. _set_show_errors_panel(false);
  1577. }
  1578. void CodeTextEditor::_set_show_errors_panel(bool p_show) {
  1579. is_errors_panel_opened = p_show;
  1580. emit_signal(SNAME("show_errors_panel"), p_show);
  1581. }
  1582. void CodeTextEditor::_set_show_warnings_panel(bool p_show) {
  1583. is_warnings_panel_opened = p_show;
  1584. emit_signal(SNAME("show_warnings_panel"), p_show);
  1585. }
  1586. void CodeTextEditor::_toggle_scripts_pressed() {
  1587. ScriptEditor::get_singleton()->toggle_scripts_panel();
  1588. update_toggle_scripts_button();
  1589. }
  1590. int CodeTextEditor::_get_affected_lines_from(int p_caret) {
  1591. return text_editor->has_selection(p_caret) ? text_editor->get_selection_from_line(p_caret) : text_editor->get_caret_line(p_caret);
  1592. }
  1593. int CodeTextEditor::_get_affected_lines_to(int p_caret) {
  1594. if (!text_editor->has_selection(p_caret)) {
  1595. return text_editor->get_caret_line(p_caret);
  1596. }
  1597. int line = text_editor->get_selection_to_line(p_caret);
  1598. // Don't affect a line with no selected characters.
  1599. if (text_editor->get_selection_to_column(p_caret) == 0) {
  1600. line--;
  1601. }
  1602. return line;
  1603. }
  1604. void CodeTextEditor::_error_pressed(const Ref<InputEvent> &p_event) {
  1605. Ref<InputEventMouseButton> mb = p_event;
  1606. if (mb.is_valid() && mb->is_pressed() && mb->get_button_index() == MouseButton::LEFT) {
  1607. goto_error();
  1608. }
  1609. }
  1610. void CodeTextEditor::_update_status_bar_theme() {
  1611. error_button->set_icon(get_theme_icon(SNAME("StatusError"), SNAME("EditorIcons")));
  1612. error_button->add_theme_color_override("font_color", get_theme_color(SNAME("error_color"), SNAME("Editor")));
  1613. error_button->add_theme_font_override("font", get_theme_font(SNAME("status_source"), SNAME("EditorFonts")));
  1614. error_button->add_theme_font_size_override("font_size", get_theme_font_size(SNAME("status_source_size"), SNAME("EditorFonts")));
  1615. warning_button->set_icon(get_theme_icon(SNAME("NodeWarning"), SNAME("EditorIcons")));
  1616. warning_button->add_theme_color_override("font_color", get_theme_color(SNAME("warning_color"), SNAME("Editor")));
  1617. warning_button->add_theme_font_override("font", get_theme_font(SNAME("status_source"), SNAME("EditorFonts")));
  1618. warning_button->add_theme_font_size_override("font_size", get_theme_font_size(SNAME("status_source_size"), SNAME("EditorFonts")));
  1619. line_and_col_txt->add_theme_font_override("font", get_theme_font(SNAME("status_source"), SNAME("EditorFonts")));
  1620. line_and_col_txt->add_theme_font_size_override("font_size", get_theme_font_size(SNAME("status_source_size"), SNAME("EditorFonts")));
  1621. }
  1622. void CodeTextEditor::_notification(int p_what) {
  1623. switch (p_what) {
  1624. case NOTIFICATION_ENTER_TREE: {
  1625. _update_status_bar_theme();
  1626. } break;
  1627. case NOTIFICATION_THEME_CHANGED: {
  1628. _update_status_bar_theme();
  1629. if (toggle_scripts_button->is_visible()) {
  1630. update_toggle_scripts_button();
  1631. }
  1632. _update_text_editor_theme();
  1633. } break;
  1634. case NOTIFICATION_VISIBILITY_CHANGED: {
  1635. if (toggle_scripts_button->is_visible()) {
  1636. update_toggle_scripts_button();
  1637. }
  1638. set_process_input(is_visible_in_tree());
  1639. } break;
  1640. case NOTIFICATION_PREDELETE: {
  1641. if (find_replace_bar) {
  1642. find_replace_bar->set_text_edit(nullptr);
  1643. }
  1644. } break;
  1645. }
  1646. }
  1647. void CodeTextEditor::set_error_count(int p_error_count) {
  1648. error_button->set_text(itos(p_error_count));
  1649. error_button->set_visible(p_error_count > 0);
  1650. if (!p_error_count) {
  1651. _set_show_errors_panel(false);
  1652. }
  1653. }
  1654. void CodeTextEditor::set_warning_count(int p_warning_count) {
  1655. warning_button->set_text(itos(p_warning_count));
  1656. warning_button->set_visible(p_warning_count > 0);
  1657. if (!p_warning_count) {
  1658. _set_show_warnings_panel(false);
  1659. }
  1660. }
  1661. void CodeTextEditor::toggle_bookmark() {
  1662. Vector<int> caret_edit_order = text_editor->get_caret_index_edit_order();
  1663. caret_edit_order.reverse();
  1664. int last_line = -1;
  1665. for (const int &c : caret_edit_order) {
  1666. int from = text_editor->has_selection(c) ? text_editor->get_selection_from_line(c) : text_editor->get_caret_line(c);
  1667. from += from == last_line ? 1 : 0;
  1668. int to = text_editor->has_selection(c) ? text_editor->get_selection_to_line(c) : text_editor->get_caret_line(c);
  1669. if (to < from) {
  1670. continue;
  1671. }
  1672. // Check first if there's any bookmarked lines in the selection.
  1673. bool selection_has_bookmarks = false;
  1674. for (int line = from; line <= to; line++) {
  1675. if (text_editor->is_line_bookmarked(line)) {
  1676. selection_has_bookmarks = true;
  1677. break;
  1678. }
  1679. }
  1680. // Set bookmark on caret or remove all bookmarks from the selection.
  1681. if (!selection_has_bookmarks) {
  1682. if (text_editor->get_caret_line(c) != last_line) {
  1683. text_editor->set_line_as_bookmarked(text_editor->get_caret_line(c), true);
  1684. }
  1685. } else {
  1686. for (int line = from; line <= to; line++) {
  1687. text_editor->set_line_as_bookmarked(line, false);
  1688. }
  1689. }
  1690. last_line = to;
  1691. }
  1692. }
  1693. void CodeTextEditor::goto_next_bookmark() {
  1694. PackedInt32Array bmarks = text_editor->get_bookmarked_lines();
  1695. if (bmarks.size() <= 0) {
  1696. return;
  1697. }
  1698. int current_line = text_editor->get_caret_line();
  1699. int bmark_idx = 0;
  1700. if (current_line < (int)bmarks[bmarks.size() - 1]) {
  1701. while (bmark_idx < bmarks.size() && bmarks[bmark_idx] <= current_line) {
  1702. bmark_idx++;
  1703. }
  1704. }
  1705. goto_line_centered(bmarks[bmark_idx]);
  1706. }
  1707. void CodeTextEditor::goto_prev_bookmark() {
  1708. PackedInt32Array bmarks = text_editor->get_bookmarked_lines();
  1709. if (bmarks.size() <= 0) {
  1710. return;
  1711. }
  1712. int current_line = text_editor->get_caret_line();
  1713. int bmark_idx = bmarks.size() - 1;
  1714. if (current_line > (int)bmarks[0]) {
  1715. while (bmark_idx >= 0 && bmarks[bmark_idx] >= current_line) {
  1716. bmark_idx--;
  1717. }
  1718. }
  1719. goto_line_centered(bmarks[bmark_idx]);
  1720. }
  1721. void CodeTextEditor::remove_all_bookmarks() {
  1722. text_editor->clear_bookmarked_lines();
  1723. }
  1724. void CodeTextEditor::_bind_methods() {
  1725. ADD_SIGNAL(MethodInfo("validate_script"));
  1726. ADD_SIGNAL(MethodInfo("load_theme_settings"));
  1727. ADD_SIGNAL(MethodInfo("show_errors_panel"));
  1728. ADD_SIGNAL(MethodInfo("show_warnings_panel"));
  1729. }
  1730. void CodeTextEditor::set_code_complete_func(CodeTextEditorCodeCompleteFunc p_code_complete_func, void *p_ud) {
  1731. code_complete_func = p_code_complete_func;
  1732. code_complete_ud = p_ud;
  1733. }
  1734. void CodeTextEditor::show_toggle_scripts_button() {
  1735. toggle_scripts_button->show();
  1736. }
  1737. void CodeTextEditor::update_toggle_scripts_button() {
  1738. if (is_layout_rtl()) {
  1739. toggle_scripts_button->set_icon(get_theme_icon(ScriptEditor::get_singleton()->is_scripts_panel_toggled() ? SNAME("Forward") : SNAME("Back"), SNAME("EditorIcons")));
  1740. } else {
  1741. toggle_scripts_button->set_icon(get_theme_icon(ScriptEditor::get_singleton()->is_scripts_panel_toggled() ? SNAME("Back") : SNAME("Forward"), SNAME("EditorIcons")));
  1742. }
  1743. toggle_scripts_button->set_tooltip_text(vformat("%s (%s)", TTR("Toggle Scripts Panel"), ED_GET_SHORTCUT("script_editor/toggle_scripts_panel")->get_as_text()));
  1744. }
  1745. CodeTextEditor::CodeTextEditor() {
  1746. code_complete_func = nullptr;
  1747. ED_SHORTCUT("script_editor/zoom_in", TTR("Zoom In"), KeyModifierMask::CMD_OR_CTRL | Key::EQUAL);
  1748. ED_SHORTCUT("script_editor/zoom_out", TTR("Zoom Out"), KeyModifierMask::CMD_OR_CTRL | Key::MINUS);
  1749. ED_SHORTCUT_ARRAY("script_editor/reset_zoom", TTR("Reset Zoom"),
  1750. { int32_t(KeyModifierMask::CMD_OR_CTRL | Key::KEY_0), int32_t(KeyModifierMask::CMD_OR_CTRL | Key::KP_0) });
  1751. text_editor = memnew(CodeEdit);
  1752. add_child(text_editor);
  1753. text_editor->set_v_size_flags(SIZE_EXPAND_FILL);
  1754. text_editor->set_structured_text_bidi_override(TextServer::STRUCTURED_TEXT_GDSCRIPT);
  1755. int ot_mode = EDITOR_GET("interface/editor/code_font_contextual_ligatures");
  1756. Ref<FontVariation> fc = text_editor->get_theme_font(SNAME("font"));
  1757. if (fc.is_valid()) {
  1758. switch (ot_mode) {
  1759. case 1: { // Disable ligatures.
  1760. Dictionary ftrs;
  1761. ftrs[TS->name_to_tag("calt")] = 0;
  1762. fc->set_opentype_features(ftrs);
  1763. } break;
  1764. case 2: { // Custom.
  1765. Vector<String> subtag = String(EDITOR_GET("interface/editor/code_font_custom_opentype_features")).split(",");
  1766. Dictionary ftrs;
  1767. for (int i = 0; i < subtag.size(); i++) {
  1768. Vector<String> subtag_a = subtag[i].split("=");
  1769. if (subtag_a.size() == 2) {
  1770. ftrs[TS->name_to_tag(subtag_a[0])] = subtag_a[1].to_int();
  1771. } else if (subtag_a.size() == 1) {
  1772. ftrs[TS->name_to_tag(subtag_a[0])] = 1;
  1773. }
  1774. }
  1775. fc->set_opentype_features(ftrs);
  1776. } break;
  1777. default: { // Enabled.
  1778. Dictionary ftrs;
  1779. ftrs[TS->name_to_tag("calt")] = 1;
  1780. fc->set_opentype_features(ftrs);
  1781. } break;
  1782. }
  1783. }
  1784. text_editor->set_draw_line_numbers(true);
  1785. text_editor->set_highlight_matching_braces_enabled(true);
  1786. text_editor->set_auto_indent_enabled(true);
  1787. text_editor->set_deselect_on_focus_loss_enabled(false);
  1788. status_bar = memnew(HBoxContainer);
  1789. add_child(status_bar);
  1790. status_bar->set_h_size_flags(SIZE_EXPAND_FILL);
  1791. status_bar->set_custom_minimum_size(Size2(0, 24 * EDSCALE)); // Adjust for the height of the warning icon.
  1792. idle = memnew(Timer);
  1793. add_child(idle);
  1794. idle->set_one_shot(true);
  1795. idle->set_wait_time(EDITOR_GET("text_editor/completion/idle_parse_delay"));
  1796. code_complete_timer = memnew(Timer);
  1797. add_child(code_complete_timer);
  1798. code_complete_timer->set_one_shot(true);
  1799. code_complete_timer->set_wait_time(EDITOR_GET("text_editor/completion/code_complete_delay"));
  1800. error_line = 0;
  1801. error_column = 0;
  1802. toggle_scripts_button = memnew(Button);
  1803. toggle_scripts_button->set_flat(true);
  1804. toggle_scripts_button->connect("pressed", callable_mp(this, &CodeTextEditor::_toggle_scripts_pressed));
  1805. status_bar->add_child(toggle_scripts_button);
  1806. toggle_scripts_button->hide();
  1807. // Error
  1808. ScrollContainer *scroll = memnew(ScrollContainer);
  1809. scroll->set_h_size_flags(SIZE_EXPAND_FILL);
  1810. scroll->set_v_size_flags(SIZE_EXPAND_FILL);
  1811. scroll->set_vertical_scroll_mode(ScrollContainer::SCROLL_MODE_DISABLED);
  1812. status_bar->add_child(scroll);
  1813. error = memnew(Label);
  1814. scroll->add_child(error);
  1815. error->set_v_size_flags(SIZE_EXPAND | SIZE_SHRINK_CENTER);
  1816. error->set_mouse_filter(MOUSE_FILTER_STOP);
  1817. error->connect("gui_input", callable_mp(this, &CodeTextEditor::_error_pressed));
  1818. // Errors
  1819. error_button = memnew(Button);
  1820. error_button->set_flat(true);
  1821. status_bar->add_child(error_button);
  1822. error_button->set_v_size_flags(SIZE_EXPAND | SIZE_SHRINK_CENTER);
  1823. error_button->set_default_cursor_shape(CURSOR_POINTING_HAND);
  1824. error_button->connect("pressed", callable_mp(this, &CodeTextEditor::_error_button_pressed));
  1825. error_button->set_tooltip_text(TTR("Errors"));
  1826. set_error_count(0);
  1827. // Warnings
  1828. warning_button = memnew(Button);
  1829. warning_button->set_flat(true);
  1830. status_bar->add_child(warning_button);
  1831. warning_button->set_v_size_flags(SIZE_EXPAND | SIZE_SHRINK_CENTER);
  1832. warning_button->set_default_cursor_shape(CURSOR_POINTING_HAND);
  1833. warning_button->connect("pressed", callable_mp(this, &CodeTextEditor::_warning_button_pressed));
  1834. warning_button->set_tooltip_text(TTR("Warnings"));
  1835. set_warning_count(0);
  1836. // Line and column
  1837. line_and_col_txt = memnew(Label);
  1838. status_bar->add_child(line_and_col_txt);
  1839. line_and_col_txt->set_v_size_flags(SIZE_EXPAND | SIZE_SHRINK_CENTER);
  1840. line_and_col_txt->set_tooltip_text(TTR("Line and column numbers."));
  1841. line_and_col_txt->set_mouse_filter(MOUSE_FILTER_STOP);
  1842. text_editor->connect("gui_input", callable_mp(this, &CodeTextEditor::_text_editor_gui_input));
  1843. text_editor->connect("caret_changed", callable_mp(this, &CodeTextEditor::_line_col_changed));
  1844. text_editor->connect("text_changed", callable_mp(this, &CodeTextEditor::_text_changed));
  1845. text_editor->connect("code_completion_requested", callable_mp(this, &CodeTextEditor::_complete_request));
  1846. TypedArray<String> cs;
  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. cs.push_back("\'");
  1855. text_editor->set_code_completion_prefixes(cs);
  1856. idle->connect("timeout", callable_mp(this, &CodeTextEditor::_text_changed_idle_timeout));
  1857. code_complete_timer->connect("timeout", callable_mp(this, &CodeTextEditor::_code_complete_timer_timeout));
  1858. font_resize_val = 0;
  1859. font_size = EDITOR_GET("interface/editor/code_font_size");
  1860. font_resize_timer = memnew(Timer);
  1861. add_child(font_resize_timer);
  1862. font_resize_timer->set_one_shot(true);
  1863. font_resize_timer->set_wait_time(0.07);
  1864. font_resize_timer->connect("timeout", callable_mp(this, &CodeTextEditor::_font_resize_timeout));
  1865. EditorSettings::get_singleton()->connect("settings_changed", callable_mp(this, &CodeTextEditor::_on_settings_change));
  1866. add_theme_constant_override("separation", 4 * EDSCALE);
  1867. }