code_editor.cpp 71 KB

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