code_editor.cpp 67 KB

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