2
0

code_editor.cpp 68 KB

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