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->remove_secondary_carets();
  171. bool selection_enabled = text_editor->has_selection(0);
  172. Point2i selection_begin, selection_end;
  173. if (selection_enabled) {
  174. selection_begin = Point2i(text_editor->get_selection_from_line(0), text_editor->get_selection_from_column(0));
  175. selection_end = Point2i(text_editor->get_selection_to_line(0), text_editor->get_selection_to_column(0));
  176. }
  177. String repl_text = get_replace_text();
  178. int search_text_len = get_search_text().length();
  179. text_editor->begin_complex_operation();
  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->remove_secondary_carets();
  215. text_editor->disconnect("text_changed", callable_mp(this, &FindReplaceBar::_editor_text_changed));
  216. // Line as x so it gets priority in comparison, column as y.
  217. Point2i orig_cursor(text_editor->get_caret_line(0), text_editor->get_caret_column(0));
  218. Point2i prev_match = Point2(-1, -1);
  219. bool selection_enabled = text_editor->has_selection(0);
  220. if (!is_selection_only()) {
  221. text_editor->deselect();
  222. selection_enabled = false;
  223. } else {
  224. result_line = -1;
  225. result_col = -1;
  226. }
  227. Point2i selection_begin, selection_end;
  228. if (selection_enabled) {
  229. selection_begin = Point2i(text_editor->get_selection_from_line(0), text_editor->get_selection_from_column(0));
  230. selection_end = Point2i(text_editor->get_selection_to_line(0), text_editor->get_selection_to_column(0));
  231. }
  232. int vsval = text_editor->get_v_scroll();
  233. String repl_text = get_replace_text();
  234. int search_text_len = get_search_text().length();
  235. int rc = 0;
  236. replace_all_mode = true;
  237. text_editor->begin_complex_operation();
  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. }
  509. }
  510. String FindReplaceBar::get_search_text() const {
  511. return search_text->get_text();
  512. }
  513. String FindReplaceBar::get_replace_text() const {
  514. return replace_text->get_text();
  515. }
  516. bool FindReplaceBar::is_case_sensitive() const {
  517. return case_sensitive->is_pressed();
  518. }
  519. bool FindReplaceBar::is_whole_words() const {
  520. return whole_words->is_pressed();
  521. }
  522. bool FindReplaceBar::is_selection_only() const {
  523. return selection_only->is_pressed();
  524. }
  525. void FindReplaceBar::set_error(const String &p_label) {
  526. emit_signal(SNAME("error"), p_label);
  527. }
  528. void FindReplaceBar::set_text_edit(CodeTextEditor *p_text_editor) {
  529. if (p_text_editor == base_text_editor) {
  530. return;
  531. }
  532. if (base_text_editor) {
  533. base_text_editor->remove_find_replace_bar();
  534. base_text_editor = nullptr;
  535. text_editor->disconnect("text_changed", callable_mp(this, &FindReplaceBar::_editor_text_changed));
  536. text_editor = nullptr;
  537. }
  538. if (!p_text_editor) {
  539. return;
  540. }
  541. results_count = -1;
  542. results_count_to_current = -1;
  543. needs_to_count_results = true;
  544. base_text_editor = p_text_editor;
  545. text_editor = base_text_editor->get_text_editor();
  546. text_editor->connect("text_changed", callable_mp(this, &FindReplaceBar::_editor_text_changed));
  547. _update_results_count();
  548. _update_matches_display();
  549. }
  550. void FindReplaceBar::_bind_methods() {
  551. ClassDB::bind_method("_search_current", &FindReplaceBar::search_current);
  552. ADD_SIGNAL(MethodInfo("error"));
  553. }
  554. FindReplaceBar::FindReplaceBar() {
  555. vbc_lineedit = memnew(VBoxContainer);
  556. add_child(vbc_lineedit);
  557. vbc_lineedit->set_alignment(BoxContainer::ALIGNMENT_CENTER);
  558. vbc_lineedit->set_h_size_flags(SIZE_EXPAND_FILL);
  559. VBoxContainer *vbc_button = memnew(VBoxContainer);
  560. add_child(vbc_button);
  561. VBoxContainer *vbc_option = memnew(VBoxContainer);
  562. add_child(vbc_option);
  563. HBoxContainer *hbc_button_search = memnew(HBoxContainer);
  564. vbc_button->add_child(hbc_button_search);
  565. hbc_button_search->set_alignment(BoxContainer::ALIGNMENT_END);
  566. hbc_button_replace = memnew(HBoxContainer);
  567. vbc_button->add_child(hbc_button_replace);
  568. hbc_button_replace->set_alignment(BoxContainer::ALIGNMENT_END);
  569. HBoxContainer *hbc_option_search = memnew(HBoxContainer);
  570. vbc_option->add_child(hbc_option_search);
  571. hbc_option_replace = memnew(HBoxContainer);
  572. vbc_option->add_child(hbc_option_replace);
  573. // Search toolbar
  574. search_text = memnew(LineEdit);
  575. vbc_lineedit->add_child(search_text);
  576. search_text->set_placeholder(TTR("Find"));
  577. search_text->set_tooltip_text(TTR("Find"));
  578. search_text->set_custom_minimum_size(Size2(100 * EDSCALE, 0));
  579. search_text->connect("text_changed", callable_mp(this, &FindReplaceBar::_search_text_changed));
  580. search_text->connect("text_submitted", callable_mp(this, &FindReplaceBar::_search_text_submitted));
  581. search_text->connect("focus_exited", callable_mp(this, &FindReplaceBar::_focus_lost));
  582. matches_label = memnew(Label);
  583. hbc_button_search->add_child(matches_label);
  584. matches_label->hide();
  585. find_prev = memnew(Button);
  586. find_prev->set_flat(true);
  587. find_prev->set_tooltip_text(TTR("Previous Match"));
  588. hbc_button_search->add_child(find_prev);
  589. find_prev->set_focus_mode(FOCUS_NONE);
  590. find_prev->connect("pressed", callable_mp(this, &FindReplaceBar::search_prev));
  591. find_next = memnew(Button);
  592. find_next->set_flat(true);
  593. find_next->set_tooltip_text(TTR("Next Match"));
  594. hbc_button_search->add_child(find_next);
  595. find_next->set_focus_mode(FOCUS_NONE);
  596. find_next->connect("pressed", callable_mp(this, &FindReplaceBar::search_next));
  597. case_sensitive = memnew(CheckBox);
  598. hbc_option_search->add_child(case_sensitive);
  599. case_sensitive->set_text(TTR("Match Case"));
  600. case_sensitive->set_focus_mode(FOCUS_NONE);
  601. case_sensitive->connect("toggled", callable_mp(this, &FindReplaceBar::_search_options_changed));
  602. whole_words = memnew(CheckBox);
  603. hbc_option_search->add_child(whole_words);
  604. whole_words->set_text(TTR("Whole Words"));
  605. whole_words->set_focus_mode(FOCUS_NONE);
  606. whole_words->connect("toggled", callable_mp(this, &FindReplaceBar::_search_options_changed));
  607. // Replace toolbar
  608. replace_text = memnew(LineEdit);
  609. vbc_lineedit->add_child(replace_text);
  610. replace_text->set_placeholder(TTR("Replace"));
  611. replace_text->set_tooltip_text(TTR("Replace"));
  612. replace_text->set_custom_minimum_size(Size2(100 * EDSCALE, 0));
  613. replace_text->connect("text_submitted", callable_mp(this, &FindReplaceBar::_replace_text_submitted));
  614. replace_text->connect("focus_exited", callable_mp(this, &FindReplaceBar::_focus_lost));
  615. replace = memnew(Button);
  616. hbc_button_replace->add_child(replace);
  617. replace->set_text(TTR("Replace"));
  618. replace->connect("pressed", callable_mp(this, &FindReplaceBar::_replace));
  619. replace_all = memnew(Button);
  620. hbc_button_replace->add_child(replace_all);
  621. replace_all->set_text(TTR("Replace All"));
  622. replace_all->connect("pressed", callable_mp(this, &FindReplaceBar::_replace_all));
  623. selection_only = memnew(CheckBox);
  624. hbc_option_replace->add_child(selection_only);
  625. selection_only->set_text(TTR("Selection Only"));
  626. selection_only->set_focus_mode(FOCUS_NONE);
  627. selection_only->connect("toggled", callable_mp(this, &FindReplaceBar::_search_options_changed));
  628. hide_button = memnew(TextureButton);
  629. add_child(hide_button);
  630. hide_button->set_tooltip_text(TTR("Hide"));
  631. hide_button->set_focus_mode(FOCUS_NONE);
  632. hide_button->connect("pressed", callable_mp(this, &FindReplaceBar::_hide_bar).bind(false));
  633. hide_button->set_v_size_flags(SIZE_SHRINK_CENTER);
  634. }
  635. /*** CODE EDITOR ****/
  636. static constexpr float ZOOM_FACTOR_PRESETS[7] = { 0.25f, 0.5f, 0.75f, 1.0f, 1.5f, 2.0f, 3.0f };
  637. // This function should be used to handle shortcuts that could otherwise
  638. // be handled too late if they weren't handled here.
  639. void CodeTextEditor::input(const Ref<InputEvent> &event) {
  640. ERR_FAIL_COND(event.is_null());
  641. const Ref<InputEventKey> key_event = event;
  642. if (!key_event.is_valid()) {
  643. return;
  644. }
  645. if (!key_event->is_pressed()) {
  646. return;
  647. }
  648. if (!text_editor->has_focus()) {
  649. 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())))) {
  650. if (ED_IS_SHORTCUT("script_text_editor/find_next", key_event)) {
  651. find_replace_bar->search_next();
  652. accept_event();
  653. return;
  654. }
  655. if (ED_IS_SHORTCUT("script_text_editor/find_previous", key_event)) {
  656. find_replace_bar->search_prev();
  657. accept_event();
  658. return;
  659. }
  660. }
  661. return;
  662. }
  663. if (ED_IS_SHORTCUT("script_text_editor/move_up", key_event)) {
  664. text_editor->move_lines_up();
  665. accept_event();
  666. return;
  667. }
  668. if (ED_IS_SHORTCUT("script_text_editor/move_down", key_event)) {
  669. text_editor->move_lines_down();
  670. accept_event();
  671. return;
  672. }
  673. if (ED_IS_SHORTCUT("script_text_editor/delete_line", key_event)) {
  674. text_editor->delete_lines();
  675. accept_event();
  676. return;
  677. }
  678. if (ED_IS_SHORTCUT("script_text_editor/duplicate_selection", key_event)) {
  679. text_editor->duplicate_selection();
  680. accept_event();
  681. return;
  682. }
  683. if (ED_IS_SHORTCUT("script_text_editor/duplicate_lines", key_event)) {
  684. text_editor->duplicate_lines();
  685. accept_event();
  686. return;
  687. }
  688. }
  689. void CodeTextEditor::_text_editor_gui_input(const Ref<InputEvent> &p_event) {
  690. Ref<InputEventMouseButton> mb = p_event;
  691. if (mb.is_valid()) {
  692. if (mb->is_pressed() && mb->is_command_or_control_pressed()) {
  693. if (mb->get_button_index() == MouseButton::WHEEL_UP) {
  694. _zoom_in();
  695. accept_event();
  696. return;
  697. }
  698. if (mb->get_button_index() == MouseButton::WHEEL_DOWN) {
  699. _zoom_out();
  700. accept_event();
  701. return;
  702. }
  703. }
  704. }
  705. Ref<InputEventMagnifyGesture> magnify_gesture = p_event;
  706. if (magnify_gesture.is_valid()) {
  707. _zoom_to(zoom_factor * powf(magnify_gesture->get_factor(), 0.25f));
  708. accept_event();
  709. return;
  710. }
  711. Ref<InputEventKey> k = p_event;
  712. if (k.is_valid()) {
  713. if (k->is_pressed()) {
  714. if (ED_IS_SHORTCUT("script_editor/zoom_in", p_event)) {
  715. _zoom_in();
  716. accept_event();
  717. return;
  718. }
  719. if (ED_IS_SHORTCUT("script_editor/zoom_out", p_event)) {
  720. _zoom_out();
  721. accept_event();
  722. return;
  723. }
  724. if (ED_IS_SHORTCUT("script_editor/reset_zoom", p_event)) {
  725. _zoom_to(1);
  726. accept_event();
  727. return;
  728. }
  729. }
  730. }
  731. }
  732. void CodeTextEditor::_line_col_changed() {
  733. if (!code_complete_timer->is_stopped() && code_complete_timer_line != text_editor->get_caret_line()) {
  734. code_complete_timer->stop();
  735. }
  736. String line = text_editor->get_line(text_editor->get_caret_line());
  737. int positional_column = 0;
  738. for (int i = 0; i < text_editor->get_caret_column(); i++) {
  739. if (line[i] == '\t') {
  740. positional_column += text_editor->get_indent_size(); // Tab size
  741. } else {
  742. positional_column += 1;
  743. }
  744. }
  745. StringBuilder sb;
  746. sb.append(itos(text_editor->get_caret_line() + 1).lpad(4));
  747. sb.append(" : ");
  748. sb.append(itos(positional_column + 1).lpad(3));
  749. line_and_col_txt->set_text(sb.as_string());
  750. if (find_replace_bar) {
  751. if (!find_replace_bar->line_col_changed_for_result) {
  752. find_replace_bar->needs_to_count_results = true;
  753. }
  754. find_replace_bar->line_col_changed_for_result = false;
  755. }
  756. }
  757. void CodeTextEditor::_text_changed() {
  758. if (code_complete_enabled && text_editor->is_insert_text_operation()) {
  759. code_complete_timer_line = text_editor->get_caret_line();
  760. code_complete_timer->start();
  761. }
  762. idle->start();
  763. if (find_replace_bar) {
  764. find_replace_bar->needs_to_count_results = true;
  765. }
  766. }
  767. void CodeTextEditor::_code_complete_timer_timeout() {
  768. if (!is_visible_in_tree()) {
  769. return;
  770. }
  771. text_editor->request_code_completion();
  772. }
  773. void CodeTextEditor::_complete_request() {
  774. List<ScriptLanguage::CodeCompletionOption> entries;
  775. String ctext = text_editor->get_text_for_code_completion();
  776. _code_complete_script(ctext, &entries);
  777. bool forced = false;
  778. if (code_complete_func) {
  779. code_complete_func(code_complete_ud, ctext, &entries, forced);
  780. }
  781. for (const ScriptLanguage::CodeCompletionOption &e : entries) {
  782. Color font_color = completion_font_color;
  783. if (!e.theme_color_name.is_empty() && EDITOR_GET("text_editor/completion/colorize_suggestions")) {
  784. font_color = get_theme_color(e.theme_color_name, SNAME("Editor"));
  785. } else if (e.insert_text.begins_with("\"") || e.insert_text.begins_with("\'")) {
  786. font_color = completion_string_color;
  787. } else if (e.insert_text.begins_with("##") || e.insert_text.begins_with("///")) {
  788. font_color = completion_doc_comment_color;
  789. } else if (e.insert_text.begins_with("&")) {
  790. font_color = completion_string_name_color;
  791. } else if (e.insert_text.begins_with("^")) {
  792. font_color = completion_node_path_color;
  793. } else if (e.insert_text.begins_with("#") || e.insert_text.begins_with("//")) {
  794. font_color = completion_comment_color;
  795. }
  796. 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);
  797. }
  798. text_editor->update_code_completion_options(forced);
  799. }
  800. Ref<Texture2D> CodeTextEditor::_get_completion_icon(const ScriptLanguage::CodeCompletionOption &p_option) {
  801. Ref<Texture2D> tex;
  802. switch (p_option.kind) {
  803. case ScriptLanguage::CODE_COMPLETION_KIND_CLASS: {
  804. if (has_theme_icon(p_option.display, EditorStringName(EditorIcons))) {
  805. tex = get_editor_theme_icon(p_option.display);
  806. } else {
  807. tex = get_editor_theme_icon(SNAME("Object"));
  808. }
  809. } break;
  810. case ScriptLanguage::CODE_COMPLETION_KIND_ENUM:
  811. tex = get_editor_theme_icon(SNAME("Enum"));
  812. break;
  813. case ScriptLanguage::CODE_COMPLETION_KIND_FILE_PATH:
  814. tex = get_editor_theme_icon(SNAME("File"));
  815. break;
  816. case ScriptLanguage::CODE_COMPLETION_KIND_NODE_PATH:
  817. tex = get_editor_theme_icon(SNAME("NodePath"));
  818. break;
  819. case ScriptLanguage::CODE_COMPLETION_KIND_VARIABLE:
  820. tex = get_editor_theme_icon(SNAME("Variant"));
  821. break;
  822. case ScriptLanguage::CODE_COMPLETION_KIND_CONSTANT:
  823. tex = get_editor_theme_icon(SNAME("MemberConstant"));
  824. break;
  825. case ScriptLanguage::CODE_COMPLETION_KIND_MEMBER:
  826. tex = get_editor_theme_icon(SNAME("MemberProperty"));
  827. break;
  828. case ScriptLanguage::CODE_COMPLETION_KIND_SIGNAL:
  829. tex = get_editor_theme_icon(SNAME("MemberSignal"));
  830. break;
  831. case ScriptLanguage::CODE_COMPLETION_KIND_FUNCTION:
  832. tex = get_editor_theme_icon(SNAME("MemberMethod"));
  833. break;
  834. case ScriptLanguage::CODE_COMPLETION_KIND_PLAIN_TEXT:
  835. tex = get_editor_theme_icon(SNAME("BoxMesh"));
  836. break;
  837. default:
  838. tex = get_editor_theme_icon(SNAME("String"));
  839. break;
  840. }
  841. return tex;
  842. }
  843. void CodeTextEditor::update_editor_settings() {
  844. // Theme: Highlighting
  845. completion_font_color = EDITOR_GET("text_editor/theme/highlighting/completion_font_color");
  846. completion_string_color = EDITOR_GET("text_editor/theme/highlighting/string_color");
  847. completion_string_name_color = EDITOR_GET("text_editor/theme/highlighting/gdscript/string_name_color");
  848. completion_node_path_color = EDITOR_GET("text_editor/theme/highlighting/gdscript/node_path_color");
  849. completion_comment_color = EDITOR_GET("text_editor/theme/highlighting/comment_color");
  850. completion_doc_comment_color = EDITOR_GET("text_editor/theme/highlighting/doc_comment_color");
  851. // Appearance: Caret
  852. text_editor->set_caret_type((TextEdit::CaretType)EDITOR_GET("text_editor/appearance/caret/type").operator int());
  853. text_editor->set_caret_blink_enabled(EDITOR_GET("text_editor/appearance/caret/caret_blink"));
  854. text_editor->set_caret_blink_interval(EDITOR_GET("text_editor/appearance/caret/caret_blink_interval"));
  855. text_editor->set_highlight_current_line(EDITOR_GET("text_editor/appearance/caret/highlight_current_line"));
  856. text_editor->set_highlight_all_occurrences(EDITOR_GET("text_editor/appearance/caret/highlight_all_occurrences"));
  857. // Appearance: Gutters
  858. text_editor->set_draw_line_numbers(EDITOR_GET("text_editor/appearance/gutters/show_line_numbers"));
  859. text_editor->set_line_numbers_zero_padded(EDITOR_GET("text_editor/appearance/gutters/line_numbers_zero_padded"));
  860. // Appearance: Minimap
  861. text_editor->set_draw_minimap(EDITOR_GET("text_editor/appearance/minimap/show_minimap"));
  862. text_editor->set_minimap_width((int)EDITOR_GET("text_editor/appearance/minimap/minimap_width") * EDSCALE);
  863. // Appearance: Lines
  864. text_editor->set_line_folding_enabled(EDITOR_GET("text_editor/appearance/lines/code_folding"));
  865. text_editor->set_draw_fold_gutter(EDITOR_GET("text_editor/appearance/lines/code_folding"));
  866. text_editor->set_line_wrapping_mode((TextEdit::LineWrappingMode)EDITOR_GET("text_editor/appearance/lines/word_wrap").operator int());
  867. text_editor->set_autowrap_mode((TextServer::AutowrapMode)EDITOR_GET("text_editor/appearance/lines/autowrap_mode").operator int());
  868. // Appearance: Whitespace
  869. text_editor->set_draw_tabs(EDITOR_GET("text_editor/appearance/whitespace/draw_tabs"));
  870. text_editor->set_draw_spaces(EDITOR_GET("text_editor/appearance/whitespace/draw_spaces"));
  871. text_editor->add_theme_constant_override("line_spacing", EDITOR_GET("text_editor/appearance/whitespace/line_spacing"));
  872. // Behavior: Navigation
  873. text_editor->set_scroll_past_end_of_file_enabled(EDITOR_GET("text_editor/behavior/navigation/scroll_past_end_of_file"));
  874. text_editor->set_smooth_scroll_enabled(EDITOR_GET("text_editor/behavior/navigation/smooth_scrolling"));
  875. text_editor->set_v_scroll_speed(EDITOR_GET("text_editor/behavior/navigation/v_scroll_speed"));
  876. text_editor->set_drag_and_drop_selection_enabled(EDITOR_GET("text_editor/behavior/navigation/drag_and_drop_selection"));
  877. // Behavior: Indent
  878. set_indent_using_spaces(EDITOR_GET("text_editor/behavior/indent/type"));
  879. text_editor->set_indent_size(EDITOR_GET("text_editor/behavior/indent/size"));
  880. text_editor->set_auto_indent_enabled(EDITOR_GET("text_editor/behavior/indent/auto_indent"));
  881. text_editor->set_indent_wrapped_lines(EDITOR_GET("text_editor/behavior/indent/indent_wrapped_lines"));
  882. // Completion
  883. text_editor->set_auto_brace_completion_enabled(EDITOR_GET("text_editor/completion/auto_brace_complete"));
  884. text_editor->set_code_hint_draw_below(EDITOR_GET("text_editor/completion/put_callhint_tooltip_below_current_line"));
  885. code_complete_enabled = EDITOR_GET("text_editor/completion/code_complete_enabled");
  886. code_complete_timer->set_wait_time(EDITOR_GET("text_editor/completion/code_complete_delay"));
  887. idle->set_wait_time(EDITOR_GET("text_editor/completion/idle_parse_delay"));
  888. // Appearance: Guidelines
  889. if (EDITOR_GET("text_editor/appearance/guidelines/show_line_length_guidelines")) {
  890. TypedArray<int> guideline_cols;
  891. guideline_cols.append(EDITOR_GET("text_editor/appearance/guidelines/line_length_guideline_hard_column"));
  892. if (EDITOR_GET("text_editor/appearance/guidelines/line_length_guideline_soft_column") != guideline_cols[0]) {
  893. guideline_cols.append(EDITOR_GET("text_editor/appearance/guidelines/line_length_guideline_soft_column"));
  894. }
  895. text_editor->set_line_length_guidelines(guideline_cols);
  896. } else {
  897. text_editor->set_line_length_guidelines(TypedArray<int>());
  898. }
  899. set_zoom_factor(zoom_factor);
  900. }
  901. void CodeTextEditor::set_find_replace_bar(FindReplaceBar *p_bar) {
  902. if (find_replace_bar) {
  903. return;
  904. }
  905. find_replace_bar = p_bar;
  906. find_replace_bar->set_text_edit(this);
  907. find_replace_bar->connect("error", callable_mp(error, &Label::set_text));
  908. }
  909. void CodeTextEditor::remove_find_replace_bar() {
  910. if (!find_replace_bar) {
  911. return;
  912. }
  913. find_replace_bar->disconnect("error", callable_mp(error, &Label::set_text));
  914. find_replace_bar = nullptr;
  915. }
  916. void CodeTextEditor::trim_trailing_whitespace() {
  917. bool trimmed_whitespace = false;
  918. for (int i = 0; i < text_editor->get_line_count(); i++) {
  919. String line = text_editor->get_line(i);
  920. if (line.ends_with(" ") || line.ends_with("\t")) {
  921. if (!trimmed_whitespace) {
  922. text_editor->begin_complex_operation();
  923. trimmed_whitespace = true;
  924. }
  925. int end = 0;
  926. for (int j = line.length() - 1; j > -1; j--) {
  927. if (line[j] != ' ' && line[j] != '\t') {
  928. end = j + 1;
  929. break;
  930. }
  931. }
  932. text_editor->remove_text(i, end, i, line.length());
  933. }
  934. }
  935. if (trimmed_whitespace) {
  936. text_editor->merge_overlapping_carets();
  937. text_editor->end_complex_operation();
  938. }
  939. }
  940. void CodeTextEditor::insert_final_newline() {
  941. int final_line = text_editor->get_line_count() - 1;
  942. String line = text_editor->get_line(final_line);
  943. // Length 0 means it's already an empty line, no need to add a newline.
  944. if (line.length() > 0 && !line.ends_with("\n")) {
  945. text_editor->insert_text("\n", final_line, line.length(), false);
  946. }
  947. }
  948. void CodeTextEditor::convert_case(CaseStyle p_case) {
  949. if (!text_editor->has_selection()) {
  950. return;
  951. }
  952. text_editor->begin_complex_operation();
  953. text_editor->begin_multicaret_edit();
  954. for (int c = 0; c < text_editor->get_caret_count(); c++) {
  955. if (text_editor->multicaret_edit_ignore_caret(c)) {
  956. continue;
  957. }
  958. if (!text_editor->has_selection(c)) {
  959. continue;
  960. }
  961. int begin = text_editor->get_selection_from_line(c);
  962. int end = text_editor->get_selection_to_line(c);
  963. int begin_col = text_editor->get_selection_from_column(c);
  964. int end_col = text_editor->get_selection_to_column(c);
  965. for (int i = begin; i <= end; i++) {
  966. int len = text_editor->get_line(i).length();
  967. if (i == end) {
  968. len = end_col;
  969. }
  970. if (i == begin) {
  971. len -= begin_col;
  972. }
  973. String new_line = text_editor->get_line(i).substr(i == begin ? begin_col : 0, len);
  974. switch (p_case) {
  975. case UPPER: {
  976. new_line = new_line.to_upper();
  977. } break;
  978. case LOWER: {
  979. new_line = new_line.to_lower();
  980. } break;
  981. case CAPITALIZE: {
  982. new_line = new_line.capitalize();
  983. } break;
  984. }
  985. if (i == begin) {
  986. new_line = text_editor->get_line(i).left(begin_col) + new_line;
  987. }
  988. if (i == end) {
  989. new_line = new_line + text_editor->get_line(i).substr(end_col);
  990. }
  991. text_editor->set_line(i, new_line);
  992. }
  993. }
  994. text_editor->end_multicaret_edit();
  995. text_editor->end_complex_operation();
  996. }
  997. void CodeTextEditor::set_indent_using_spaces(bool p_use_spaces) {
  998. text_editor->set_indent_using_spaces(p_use_spaces);
  999. indentation_txt->set_text(p_use_spaces ? TTR("Spaces", "Indentation") : TTR("Tabs", "Indentation"));
  1000. }
  1001. void CodeTextEditor::toggle_inline_comment(const String &delimiter) {
  1002. text_editor->begin_complex_operation();
  1003. text_editor->begin_multicaret_edit();
  1004. Vector<Point2i> line_ranges = text_editor->get_line_ranges_from_carets();
  1005. int folded_to = 0;
  1006. for (Point2i line_range : line_ranges) {
  1007. int from_line = line_range.x;
  1008. int to_line = line_range.y;
  1009. // If last line is folded, extends to the end of the folded section
  1010. if (text_editor->is_line_folded(to_line)) {
  1011. folded_to = text_editor->get_next_visible_line_offset_from(to_line + 1, 1) - 1;
  1012. to_line += folded_to;
  1013. }
  1014. // Check first if there's any uncommented lines in selection.
  1015. bool is_commented = true;
  1016. bool is_all_empty = true;
  1017. for (int line = from_line; line <= to_line; line++) {
  1018. // `+ delimiter.length()` here because comment delimiter is not actually `in comment` so we check first character after it
  1019. int delimiter_idx = text_editor->is_in_comment(line, text_editor->get_first_non_whitespace_column(line) + delimiter.length());
  1020. // Empty lines should not be counted.
  1021. bool is_empty = text_editor->get_line(line).strip_edges().is_empty();
  1022. is_all_empty = is_all_empty && is_empty;
  1023. // get_delimiter_start_key will return `##` instead of `#` when there is multiple comment delimiter in a line.
  1024. if (!is_empty && (delimiter_idx == -1 || !text_editor->get_delimiter_start_key(delimiter_idx).begins_with(delimiter))) {
  1025. is_commented = false;
  1026. break;
  1027. }
  1028. }
  1029. // Special case for commenting empty lines, treat it/them as uncommented lines.
  1030. is_commented = is_commented && !is_all_empty;
  1031. // Comment/uncomment.
  1032. for (int line = from_line; line <= to_line; line++) {
  1033. if (is_all_empty) {
  1034. text_editor->insert_text(delimiter, line, 0);
  1035. continue;
  1036. }
  1037. if (is_commented) {
  1038. int delimiter_column = text_editor->get_line(line).find(delimiter);
  1039. text_editor->remove_text(line, delimiter_column, line, delimiter_column + delimiter.length());
  1040. } else {
  1041. text_editor->insert_text(delimiter, line, text_editor->get_first_non_whitespace_column(line));
  1042. }
  1043. }
  1044. }
  1045. text_editor->end_multicaret_edit();
  1046. text_editor->end_complex_operation();
  1047. }
  1048. void CodeTextEditor::goto_line(int p_line) {
  1049. text_editor->remove_secondary_carets();
  1050. text_editor->deselect();
  1051. text_editor->unfold_line(p_line);
  1052. callable_mp((TextEdit *)text_editor, &TextEdit::set_caret_line).call_deferred(p_line, true, true, 0, 0);
  1053. }
  1054. void CodeTextEditor::goto_line_selection(int p_line, int p_begin, int p_end) {
  1055. text_editor->remove_secondary_carets();
  1056. text_editor->unfold_line(p_line);
  1057. callable_mp((TextEdit *)text_editor, &TextEdit::set_caret_line).call_deferred(p_line, true, true, 0, 0);
  1058. callable_mp((TextEdit *)text_editor, &TextEdit::set_caret_column).call_deferred(p_begin, true, 0);
  1059. text_editor->select(p_line, p_begin, p_line, p_end);
  1060. }
  1061. void CodeTextEditor::goto_line_centered(int p_line) {
  1062. goto_line(p_line);
  1063. callable_mp((TextEdit *)text_editor, &TextEdit::center_viewport_to_caret).call_deferred(0);
  1064. }
  1065. void CodeTextEditor::set_executing_line(int p_line) {
  1066. text_editor->set_line_as_executing(p_line, true);
  1067. }
  1068. void CodeTextEditor::clear_executing_line() {
  1069. text_editor->clear_executing_lines();
  1070. }
  1071. Variant CodeTextEditor::get_edit_state() {
  1072. Dictionary state;
  1073. state.merge(get_navigation_state());
  1074. state["folded_lines"] = text_editor->get_folded_lines();
  1075. state["breakpoints"] = text_editor->get_breakpointed_lines();
  1076. state["bookmarks"] = text_editor->get_bookmarked_lines();
  1077. Ref<EditorSyntaxHighlighter> syntax_highlighter = text_editor->get_syntax_highlighter();
  1078. state["syntax_highlighter"] = syntax_highlighter->_get_name();
  1079. return state;
  1080. }
  1081. Variant CodeTextEditor::get_previous_state() {
  1082. return previous_state;
  1083. }
  1084. void CodeTextEditor::store_previous_state() {
  1085. previous_state = get_navigation_state();
  1086. }
  1087. void CodeTextEditor::set_edit_state(const Variant &p_state) {
  1088. Dictionary state = p_state;
  1089. /* update the row first as it sets the column to 0 */
  1090. text_editor->set_caret_line(state["row"]);
  1091. text_editor->set_caret_column(state["column"]);
  1092. if (int(state["scroll_position"]) == -1) {
  1093. // Special case for previous state.
  1094. text_editor->center_viewport_to_caret();
  1095. } else {
  1096. text_editor->set_v_scroll(state["scroll_position"]);
  1097. }
  1098. text_editor->set_h_scroll(state["h_scroll_position"]);
  1099. if (state.get("selection", false)) {
  1100. text_editor->select(state["selection_from_line"], state["selection_from_column"], state["selection_to_line"], state["selection_to_column"]);
  1101. } else {
  1102. text_editor->deselect();
  1103. }
  1104. if (state.has("folded_lines")) {
  1105. Vector<int> folded_lines = state["folded_lines"];
  1106. for (int i = 0; i < folded_lines.size(); i++) {
  1107. text_editor->fold_line(folded_lines[i]);
  1108. }
  1109. }
  1110. if (state.has("breakpoints")) {
  1111. Array breakpoints = state["breakpoints"];
  1112. for (int i = 0; i < breakpoints.size(); i++) {
  1113. text_editor->set_line_as_breakpoint(breakpoints[i], true);
  1114. }
  1115. }
  1116. if (state.has("bookmarks")) {
  1117. Array bookmarks = state["bookmarks"];
  1118. for (int i = 0; i < bookmarks.size(); i++) {
  1119. text_editor->set_line_as_bookmarked(bookmarks[i], true);
  1120. }
  1121. }
  1122. if (previous_state.is_empty()) {
  1123. previous_state = p_state;
  1124. }
  1125. }
  1126. Variant CodeTextEditor::get_navigation_state() {
  1127. Dictionary state;
  1128. state["scroll_position"] = text_editor->get_v_scroll();
  1129. state["h_scroll_position"] = text_editor->get_h_scroll();
  1130. state["column"] = text_editor->get_caret_column();
  1131. state["row"] = text_editor->get_caret_line();
  1132. state["selection"] = get_text_editor()->has_selection();
  1133. if (get_text_editor()->has_selection()) {
  1134. state["selection_from_line"] = text_editor->get_selection_from_line();
  1135. state["selection_from_column"] = text_editor->get_selection_from_column();
  1136. state["selection_to_line"] = text_editor->get_selection_to_line();
  1137. state["selection_to_column"] = text_editor->get_selection_to_column();
  1138. }
  1139. return state;
  1140. }
  1141. void CodeTextEditor::set_error(const String &p_error) {
  1142. error->set_text(p_error);
  1143. if (!p_error.is_empty()) {
  1144. error->set_default_cursor_shape(CURSOR_POINTING_HAND);
  1145. } else {
  1146. error->set_default_cursor_shape(CURSOR_ARROW);
  1147. }
  1148. }
  1149. void CodeTextEditor::set_error_pos(int p_line, int p_column) {
  1150. error_line = p_line;
  1151. error_column = p_column;
  1152. }
  1153. Point2i CodeTextEditor::get_error_pos() const {
  1154. return Point2i(error_line, error_column);
  1155. }
  1156. void CodeTextEditor::goto_error() {
  1157. if (!error->get_text().is_empty()) {
  1158. if (text_editor->get_line_count() != error_line) {
  1159. text_editor->unfold_line(error_line);
  1160. }
  1161. text_editor->remove_secondary_carets();
  1162. text_editor->set_caret_line(error_line);
  1163. text_editor->set_caret_column(error_column);
  1164. text_editor->center_viewport_to_caret();
  1165. }
  1166. }
  1167. void CodeTextEditor::_update_text_editor_theme() {
  1168. emit_signal(SNAME("load_theme_settings"));
  1169. error_button->set_icon(get_editor_theme_icon(SNAME("StatusError")));
  1170. warning_button->set_icon(get_editor_theme_icon(SNAME("NodeWarning")));
  1171. Ref<Font> status_bar_font = get_theme_font(SNAME("status_source"), EditorStringName(EditorFonts));
  1172. int status_bar_font_size = get_theme_font_size(SNAME("status_source_size"), EditorStringName(EditorFonts));
  1173. int count = status_bar->get_child_count();
  1174. for (int i = 0; i < count; i++) {
  1175. Control *n = Object::cast_to<Control>(status_bar->get_child(i));
  1176. if (n) {
  1177. n->add_theme_font_override(SNAME("font"), status_bar_font);
  1178. n->add_theme_font_size_override(SNAME("font_size"), status_bar_font_size);
  1179. }
  1180. }
  1181. const Color &error_color = get_theme_color(SNAME("error_color"), EditorStringName(Editor));
  1182. const Color &warning_color = get_theme_color(SNAME("warning_color"), EditorStringName(Editor));
  1183. error->add_theme_color_override(SNAME("font_color"), error_color);
  1184. error_button->add_theme_color_override(SNAME("font_color"), error_color);
  1185. warning_button->add_theme_color_override(SNAME("font_color"), warning_color);
  1186. _update_font_ligatures();
  1187. }
  1188. void CodeTextEditor::_update_font_ligatures() {
  1189. int ot_mode = EDITOR_GET("interface/editor/code_font_contextual_ligatures");
  1190. Ref<FontVariation> fc = text_editor->get_theme_font(SNAME("font"));
  1191. if (fc.is_valid()) {
  1192. switch (ot_mode) {
  1193. case 1: { // Disable ligatures.
  1194. Dictionary ftrs;
  1195. ftrs[TS->name_to_tag("calt")] = 0;
  1196. fc->set_opentype_features(ftrs);
  1197. } break;
  1198. case 2: { // Custom.
  1199. Vector<String> subtag = String(EDITOR_GET("interface/editor/code_font_custom_opentype_features")).split(",");
  1200. Dictionary ftrs;
  1201. for (int i = 0; i < subtag.size(); i++) {
  1202. Vector<String> subtag_a = subtag[i].split("=");
  1203. if (subtag_a.size() == 2) {
  1204. ftrs[TS->name_to_tag(subtag_a[0])] = subtag_a[1].to_int();
  1205. } else if (subtag_a.size() == 1) {
  1206. ftrs[TS->name_to_tag(subtag_a[0])] = 1;
  1207. }
  1208. }
  1209. fc->set_opentype_features(ftrs);
  1210. } break;
  1211. default: { // Enabled.
  1212. Dictionary ftrs;
  1213. ftrs[TS->name_to_tag("calt")] = 1;
  1214. fc->set_opentype_features(ftrs);
  1215. } break;
  1216. }
  1217. }
  1218. }
  1219. void CodeTextEditor::_text_changed_idle_timeout() {
  1220. _validate_script();
  1221. emit_signal(SNAME("validate_script"));
  1222. }
  1223. void CodeTextEditor::validate_script() {
  1224. idle->start();
  1225. }
  1226. void CodeTextEditor::_error_button_pressed() {
  1227. _set_show_errors_panel(!is_errors_panel_opened);
  1228. _set_show_warnings_panel(false);
  1229. }
  1230. void CodeTextEditor::_warning_button_pressed() {
  1231. _set_show_warnings_panel(!is_warnings_panel_opened);
  1232. _set_show_errors_panel(false);
  1233. }
  1234. void CodeTextEditor::_zoom_popup_id_pressed(int p_idx) {
  1235. _zoom_to(zoom_button->get_popup()->get_item_metadata(p_idx));
  1236. }
  1237. void CodeTextEditor::_set_show_errors_panel(bool p_show) {
  1238. is_errors_panel_opened = p_show;
  1239. emit_signal(SNAME("show_errors_panel"), p_show);
  1240. }
  1241. void CodeTextEditor::_set_show_warnings_panel(bool p_show) {
  1242. is_warnings_panel_opened = p_show;
  1243. emit_signal(SNAME("show_warnings_panel"), p_show);
  1244. }
  1245. void CodeTextEditor::_toggle_scripts_pressed() {
  1246. ScriptEditor::get_singleton()->toggle_scripts_panel();
  1247. update_toggle_scripts_button();
  1248. }
  1249. void CodeTextEditor::_error_pressed(const Ref<InputEvent> &p_event) {
  1250. Ref<InputEventMouseButton> mb = p_event;
  1251. if (mb.is_valid() && mb->is_pressed() && mb->get_button_index() == MouseButton::LEFT) {
  1252. goto_error();
  1253. }
  1254. }
  1255. void CodeTextEditor::_notification(int p_what) {
  1256. switch (p_what) {
  1257. case NOTIFICATION_THEME_CHANGED: {
  1258. if (toggle_scripts_button->is_visible()) {
  1259. update_toggle_scripts_button();
  1260. }
  1261. _update_text_editor_theme();
  1262. } break;
  1263. case NOTIFICATION_VISIBILITY_CHANGED: {
  1264. if (toggle_scripts_button->is_visible()) {
  1265. update_toggle_scripts_button();
  1266. }
  1267. set_process_input(is_visible_in_tree());
  1268. } break;
  1269. case NOTIFICATION_PREDELETE: {
  1270. if (find_replace_bar) {
  1271. find_replace_bar->set_text_edit(nullptr);
  1272. }
  1273. } break;
  1274. }
  1275. }
  1276. void CodeTextEditor::set_error_count(int p_error_count) {
  1277. error_button->set_text(itos(p_error_count));
  1278. error_button->set_visible(p_error_count > 0);
  1279. if (!p_error_count) {
  1280. _set_show_errors_panel(false);
  1281. }
  1282. }
  1283. void CodeTextEditor::set_warning_count(int p_warning_count) {
  1284. warning_button->set_text(itos(p_warning_count));
  1285. warning_button->set_visible(p_warning_count > 0);
  1286. if (!p_warning_count) {
  1287. _set_show_warnings_panel(false);
  1288. }
  1289. }
  1290. void CodeTextEditor::toggle_bookmark() {
  1291. Vector<int> sorted_carets = text_editor->get_sorted_carets();
  1292. int last_line = -1;
  1293. for (const int &c : sorted_carets) {
  1294. int from = text_editor->get_selection_from_line(c);
  1295. from += from == last_line ? 1 : 0;
  1296. int to = text_editor->get_selection_to_line(c);
  1297. if (to < from) {
  1298. continue;
  1299. }
  1300. // Check first if there's any bookmarked lines in the selection.
  1301. bool selection_has_bookmarks = false;
  1302. for (int line = from; line <= to; line++) {
  1303. if (text_editor->is_line_bookmarked(line)) {
  1304. selection_has_bookmarks = true;
  1305. break;
  1306. }
  1307. }
  1308. // Set bookmark on caret or remove all bookmarks from the selection.
  1309. if (!selection_has_bookmarks) {
  1310. if (text_editor->get_caret_line(c) != last_line) {
  1311. text_editor->set_line_as_bookmarked(text_editor->get_caret_line(c), true);
  1312. }
  1313. } else {
  1314. for (int line = from; line <= to; line++) {
  1315. text_editor->set_line_as_bookmarked(line, false);
  1316. }
  1317. }
  1318. last_line = to;
  1319. }
  1320. }
  1321. void CodeTextEditor::goto_next_bookmark() {
  1322. PackedInt32Array bmarks = text_editor->get_bookmarked_lines();
  1323. if (bmarks.size() <= 0) {
  1324. return;
  1325. }
  1326. int current_line = text_editor->get_caret_line();
  1327. int bmark_idx = 0;
  1328. if (current_line < (int)bmarks[bmarks.size() - 1]) {
  1329. while (bmark_idx < bmarks.size() && bmarks[bmark_idx] <= current_line) {
  1330. bmark_idx++;
  1331. }
  1332. }
  1333. goto_line_centered(bmarks[bmark_idx]);
  1334. }
  1335. void CodeTextEditor::goto_prev_bookmark() {
  1336. PackedInt32Array bmarks = text_editor->get_bookmarked_lines();
  1337. if (bmarks.size() <= 0) {
  1338. return;
  1339. }
  1340. int current_line = text_editor->get_caret_line();
  1341. int bmark_idx = bmarks.size() - 1;
  1342. if (current_line > (int)bmarks[0]) {
  1343. while (bmark_idx >= 0 && bmarks[bmark_idx] >= current_line) {
  1344. bmark_idx--;
  1345. }
  1346. }
  1347. goto_line_centered(bmarks[bmark_idx]);
  1348. }
  1349. void CodeTextEditor::remove_all_bookmarks() {
  1350. text_editor->clear_bookmarked_lines();
  1351. }
  1352. void CodeTextEditor::_zoom_in() {
  1353. int s = text_editor->get_theme_font_size("font_size");
  1354. _zoom_to(zoom_factor * (s + MAX(1.0f, EDSCALE)) / s);
  1355. }
  1356. void CodeTextEditor::_zoom_out() {
  1357. int s = text_editor->get_theme_font_size("font_size");
  1358. _zoom_to(zoom_factor * (s - MAX(1.0f, EDSCALE)) / s);
  1359. }
  1360. void CodeTextEditor::_zoom_to(float p_zoom_factor) {
  1361. if (zoom_factor == p_zoom_factor) {
  1362. return;
  1363. }
  1364. float old_zoom_factor = zoom_factor;
  1365. set_zoom_factor(p_zoom_factor);
  1366. if (old_zoom_factor != zoom_factor) {
  1367. emit_signal(SNAME("zoomed"), zoom_factor);
  1368. }
  1369. }
  1370. void CodeTextEditor::set_zoom_factor(float p_zoom_factor) {
  1371. int preset_count = sizeof(ZOOM_FACTOR_PRESETS) / sizeof(float);
  1372. zoom_factor = CLAMP(p_zoom_factor, ZOOM_FACTOR_PRESETS[0], ZOOM_FACTOR_PRESETS[preset_count - 1]);
  1373. int neutral_font_size = int(EDITOR_GET("interface/editor/code_font_size")) * EDSCALE;
  1374. int new_font_size = Math::round(zoom_factor * neutral_font_size);
  1375. zoom_button->set_text(itos(Math::round(zoom_factor * 100)) + " %");
  1376. if (text_editor->has_theme_font_size_override("font_size")) {
  1377. text_editor->remove_theme_font_size_override("font_size");
  1378. }
  1379. text_editor->add_theme_font_size_override("font_size", new_font_size);
  1380. }
  1381. float CodeTextEditor::get_zoom_factor() {
  1382. return zoom_factor;
  1383. }
  1384. void CodeTextEditor::_bind_methods() {
  1385. ADD_SIGNAL(MethodInfo("validate_script"));
  1386. ADD_SIGNAL(MethodInfo("load_theme_settings"));
  1387. ADD_SIGNAL(MethodInfo("show_errors_panel"));
  1388. ADD_SIGNAL(MethodInfo("show_warnings_panel"));
  1389. ADD_SIGNAL(MethodInfo("zoomed", PropertyInfo(Variant::FLOAT, "p_zoom_factor")));
  1390. }
  1391. void CodeTextEditor::set_code_complete_func(CodeTextEditorCodeCompleteFunc p_code_complete_func, void *p_ud) {
  1392. code_complete_func = p_code_complete_func;
  1393. code_complete_ud = p_ud;
  1394. }
  1395. void CodeTextEditor::show_toggle_scripts_button() {
  1396. toggle_scripts_button->show();
  1397. }
  1398. void CodeTextEditor::update_toggle_scripts_button() {
  1399. if (is_layout_rtl()) {
  1400. toggle_scripts_button->set_icon(get_editor_theme_icon(ScriptEditor::get_singleton()->is_scripts_panel_toggled() ? SNAME("Forward") : SNAME("Back")));
  1401. } else {
  1402. toggle_scripts_button->set_icon(get_editor_theme_icon(ScriptEditor::get_singleton()->is_scripts_panel_toggled() ? SNAME("Back") : SNAME("Forward")));
  1403. }
  1404. toggle_scripts_button->set_tooltip_text(vformat("%s (%s)", TTR("Toggle Scripts Panel"), ED_GET_SHORTCUT("script_editor/toggle_scripts_panel")->get_as_text()));
  1405. }
  1406. CodeTextEditor::CodeTextEditor() {
  1407. code_complete_func = nullptr;
  1408. ED_SHORTCUT("script_editor/zoom_in", TTR("Zoom In"), KeyModifierMask::CMD_OR_CTRL | Key::EQUAL);
  1409. ED_SHORTCUT("script_editor/zoom_out", TTR("Zoom Out"), KeyModifierMask::CMD_OR_CTRL | Key::MINUS);
  1410. ED_SHORTCUT_ARRAY("script_editor/reset_zoom", TTR("Reset Zoom"),
  1411. { int32_t(KeyModifierMask::CMD_OR_CTRL | Key::KEY_0), int32_t(KeyModifierMask::CMD_OR_CTRL | Key::KP_0) });
  1412. text_editor = memnew(CodeEdit);
  1413. add_child(text_editor);
  1414. text_editor->set_v_size_flags(SIZE_EXPAND_FILL);
  1415. text_editor->set_structured_text_bidi_override(TextServer::STRUCTURED_TEXT_GDSCRIPT);
  1416. text_editor->set_draw_bookmarks_gutter(true);
  1417. text_editor->set_draw_line_numbers(true);
  1418. text_editor->set_highlight_matching_braces_enabled(true);
  1419. text_editor->set_auto_indent_enabled(true);
  1420. text_editor->set_deselect_on_focus_loss_enabled(false);
  1421. status_bar = memnew(HBoxContainer);
  1422. add_child(status_bar);
  1423. status_bar->set_h_size_flags(SIZE_EXPAND_FILL);
  1424. status_bar->set_custom_minimum_size(Size2(0, 24 * EDSCALE)); // Adjust for the height of the warning icon.
  1425. idle = memnew(Timer);
  1426. add_child(idle);
  1427. idle->set_one_shot(true);
  1428. code_complete_enabled = EDITOR_GET("text_editor/completion/code_complete_enabled");
  1429. code_complete_timer = memnew(Timer);
  1430. add_child(code_complete_timer);
  1431. code_complete_timer->set_one_shot(true);
  1432. error_line = 0;
  1433. error_column = 0;
  1434. toggle_scripts_button = memnew(Button);
  1435. toggle_scripts_button->set_flat(true);
  1436. toggle_scripts_button->set_v_size_flags(SIZE_EXPAND | SIZE_SHRINK_CENTER);
  1437. toggle_scripts_button->connect("pressed", callable_mp(this, &CodeTextEditor::_toggle_scripts_pressed));
  1438. status_bar->add_child(toggle_scripts_button);
  1439. toggle_scripts_button->hide();
  1440. // Error
  1441. ScrollContainer *scroll = memnew(ScrollContainer);
  1442. scroll->set_h_size_flags(SIZE_EXPAND_FILL);
  1443. scroll->set_v_size_flags(SIZE_EXPAND_FILL);
  1444. scroll->set_vertical_scroll_mode(ScrollContainer::SCROLL_MODE_DISABLED);
  1445. status_bar->add_child(scroll);
  1446. error = memnew(Label);
  1447. scroll->add_child(error);
  1448. error->set_v_size_flags(SIZE_EXPAND | SIZE_SHRINK_CENTER);
  1449. error->set_mouse_filter(MOUSE_FILTER_STOP);
  1450. error->connect("gui_input", callable_mp(this, &CodeTextEditor::_error_pressed));
  1451. // Errors
  1452. error_button = memnew(Button);
  1453. error_button->set_flat(true);
  1454. status_bar->add_child(error_button);
  1455. error_button->set_v_size_flags(SIZE_EXPAND | SIZE_SHRINK_CENTER);
  1456. error_button->set_default_cursor_shape(CURSOR_POINTING_HAND);
  1457. error_button->connect("pressed", callable_mp(this, &CodeTextEditor::_error_button_pressed));
  1458. error_button->set_tooltip_text(TTR("Errors"));
  1459. set_error_count(0);
  1460. // Warnings
  1461. warning_button = memnew(Button);
  1462. warning_button->set_flat(true);
  1463. status_bar->add_child(warning_button);
  1464. warning_button->set_v_size_flags(SIZE_EXPAND | SIZE_SHRINK_CENTER);
  1465. warning_button->set_default_cursor_shape(CURSOR_POINTING_HAND);
  1466. warning_button->connect("pressed", callable_mp(this, &CodeTextEditor::_warning_button_pressed));
  1467. warning_button->set_tooltip_text(TTR("Warnings"));
  1468. set_warning_count(0);
  1469. status_bar->add_child(memnew(VSeparator));
  1470. // Zoom
  1471. zoom_button = memnew(MenuButton);
  1472. status_bar->add_child(zoom_button);
  1473. zoom_button->set_flat(true);
  1474. zoom_button->set_v_size_flags(SIZE_EXPAND | SIZE_SHRINK_CENTER);
  1475. zoom_button->set_tooltip_text(TTR("Zoom factor"));
  1476. zoom_button->set_text("100 %");
  1477. PopupMenu *zoom_menu = zoom_button->get_popup();
  1478. int preset_count = sizeof(ZOOM_FACTOR_PRESETS) / sizeof(float);
  1479. for (int i = 0; i < preset_count; i++) {
  1480. float z = ZOOM_FACTOR_PRESETS[i];
  1481. zoom_menu->add_item(itos(Math::round(z * 100)) + " %");
  1482. zoom_menu->set_item_metadata(i, z);
  1483. }
  1484. zoom_menu->connect("id_pressed", callable_mp(this, &CodeTextEditor::_zoom_popup_id_pressed));
  1485. status_bar->add_child(memnew(VSeparator));
  1486. // Line and column
  1487. line_and_col_txt = memnew(Label);
  1488. status_bar->add_child(line_and_col_txt);
  1489. line_and_col_txt->set_v_size_flags(SIZE_EXPAND | SIZE_SHRINK_CENTER);
  1490. line_and_col_txt->set_tooltip_text(TTR("Line and column numbers."));
  1491. line_and_col_txt->set_mouse_filter(MOUSE_FILTER_STOP);
  1492. status_bar->add_child(memnew(VSeparator));
  1493. // Indentation
  1494. indentation_txt = memnew(Label);
  1495. status_bar->add_child(indentation_txt);
  1496. indentation_txt->set_v_size_flags(SIZE_EXPAND | SIZE_SHRINK_CENTER);
  1497. indentation_txt->set_tooltip_text(TTR("Indentation"));
  1498. indentation_txt->set_mouse_filter(MOUSE_FILTER_STOP);
  1499. text_editor->connect("gui_input", callable_mp(this, &CodeTextEditor::_text_editor_gui_input));
  1500. text_editor->connect("caret_changed", callable_mp(this, &CodeTextEditor::_line_col_changed));
  1501. text_editor->connect("text_changed", callable_mp(this, &CodeTextEditor::_text_changed));
  1502. text_editor->connect("code_completion_requested", callable_mp(this, &CodeTextEditor::_complete_request));
  1503. TypedArray<String> cs;
  1504. cs.push_back(".");
  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. text_editor->set_code_completion_prefixes(cs);
  1513. idle->connect("timeout", callable_mp(this, &CodeTextEditor::_text_changed_idle_timeout));
  1514. code_complete_timer->connect("timeout", callable_mp(this, &CodeTextEditor::_code_complete_timer_timeout));
  1515. add_theme_constant_override("separation", 4 * EDSCALE);
  1516. }