code_editor.cpp 63 KB

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