code_editor.cpp 71 KB

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