code_editor.cpp 71 KB

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