code_editor.cpp 59 KB

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