code_editor.cpp 69 KB

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