code_editor.cpp 64 KB

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