code_editor.cpp 66 KB

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