code_editor.cpp 67 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979
  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_texture_normal(get_editor_theme_icon(SNAME("Close")));
  125. hide_button->set_texture_hover(get_editor_theme_icon(SNAME("Close")));
  126. hide_button->set_texture_pressed(get_editor_theme_icon(SNAME("Close")));
  127. hide_button->set_custom_minimum_size(hide_button->get_texture_normal()->get_size());
  128. _update_toggle_replace_button(replace_text->is_visible_in_tree());
  129. } break;
  130. case NOTIFICATION_LAYOUT_DIRECTION_CHANGED:
  131. case NOTIFICATION_TRANSLATION_CHANGED: {
  132. _update_toggle_replace_button(replace_text->is_visible_in_tree());
  133. } break;
  134. case NOTIFICATION_VISIBILITY_CHANGED: {
  135. set_process_input(is_visible_in_tree());
  136. } break;
  137. case NOTIFICATION_THEME_CHANGED: {
  138. 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)));
  139. } break;
  140. case NOTIFICATION_PREDELETE: {
  141. if (base_text_editor) {
  142. base_text_editor->remove_find_replace_bar();
  143. base_text_editor = nullptr;
  144. }
  145. } break;
  146. }
  147. }
  148. // Implemented in input(..) as the LineEdit consumes the Escape pressed key.
  149. void FindReplaceBar::input(const Ref<InputEvent> &p_event) {
  150. ERR_FAIL_COND(p_event.is_null());
  151. Ref<InputEventKey> k = p_event;
  152. if (k.is_valid() && k->is_action_pressed(SNAME("ui_cancel"), false, true)) {
  153. Control *focus_owner = get_viewport()->gui_get_focus_owner();
  154. if (text_editor->has_focus() || (focus_owner && is_ancestor_of(focus_owner))) {
  155. _hide_bar();
  156. accept_event();
  157. }
  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() {
  466. if (replace_text->has_focus() || search_text->has_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_accessibility_name(TTRC("Replace Mode"));
  624. toggle_replace_button->set_flat(true);
  625. toggle_replace_button->set_focus_mode(FOCUS_NONE);
  626. toggle_replace_button->connect(SceneStringName(pressed), callable_mp(this, &FindReplaceBar::_toggle_replace_pressed));
  627. vbc_lineedit = memnew(VBoxContainer);
  628. add_child(vbc_lineedit);
  629. vbc_lineedit->set_alignment(BoxContainer::ALIGNMENT_CENTER);
  630. vbc_lineedit->set_h_size_flags(SIZE_EXPAND_FILL);
  631. VBoxContainer *vbc_button = memnew(VBoxContainer);
  632. add_child(vbc_button);
  633. VBoxContainer *vbc_option = memnew(VBoxContainer);
  634. add_child(vbc_option);
  635. HBoxContainer *hbc_button_search = memnew(HBoxContainer);
  636. vbc_button->add_child(hbc_button_search);
  637. hbc_button_search->set_alignment(BoxContainer::ALIGNMENT_END);
  638. hbc_button_replace = memnew(HBoxContainer);
  639. vbc_button->add_child(hbc_button_replace);
  640. hbc_button_replace->set_alignment(BoxContainer::ALIGNMENT_END);
  641. HBoxContainer *hbc_option_search = memnew(HBoxContainer);
  642. vbc_option->add_child(hbc_option_search);
  643. hbc_option_replace = memnew(HBoxContainer);
  644. vbc_option->add_child(hbc_option_replace);
  645. // Search toolbar
  646. search_text = memnew(LineEdit);
  647. search_text->set_keep_editing_on_text_submit(true);
  648. vbc_lineedit->add_child(search_text);
  649. search_text->set_placeholder(TTR("Find"));
  650. search_text->set_tooltip_text(TTR("Find"));
  651. search_text->set_accessibility_name(TTRC("Find"));
  652. search_text->set_custom_minimum_size(Size2(100 * EDSCALE, 0));
  653. search_text->connect(SceneStringName(text_changed), callable_mp(this, &FindReplaceBar::_search_text_changed));
  654. search_text->connect(SceneStringName(text_submitted), callable_mp(this, &FindReplaceBar::_search_text_submitted));
  655. matches_label = memnew(Label);
  656. hbc_button_search->add_child(matches_label);
  657. matches_label->set_focus_mode(FOCUS_ACCESSIBILITY);
  658. matches_label->hide();
  659. find_prev = memnew(Button);
  660. find_prev->set_flat(true);
  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_tooltip_text(TTR("Next Match"));
  669. find_next->set_accessibility_name(TTRC("Next Match"));
  670. hbc_button_search->add_child(find_next);
  671. find_next->set_focus_mode(FOCUS_NONE);
  672. find_next->connect(SceneStringName(pressed), callable_mp(this, &FindReplaceBar::search_next));
  673. case_sensitive = memnew(CheckBox);
  674. hbc_option_search->add_child(case_sensitive);
  675. case_sensitive->set_text(TTR("Match Case"));
  676. case_sensitive->set_focus_mode(FOCUS_NONE);
  677. case_sensitive->connect(SceneStringName(toggled), callable_mp(this, &FindReplaceBar::_search_options_changed));
  678. whole_words = memnew(CheckBox);
  679. hbc_option_search->add_child(whole_words);
  680. whole_words->set_text(TTR("Whole Words"));
  681. whole_words->set_focus_mode(FOCUS_NONE);
  682. whole_words->connect(SceneStringName(toggled), callable_mp(this, &FindReplaceBar::_search_options_changed));
  683. // Replace toolbar
  684. replace_text = memnew(LineEdit);
  685. vbc_lineedit->add_child(replace_text);
  686. replace_text->set_placeholder(TTR("Replace"));
  687. replace_text->set_tooltip_text(TTR("Replace"));
  688. replace_text->set_accessibility_name(TTRC("Replace"));
  689. replace_text->set_custom_minimum_size(Size2(100 * EDSCALE, 0));
  690. replace_text->connect(SceneStringName(text_submitted), callable_mp(this, &FindReplaceBar::_replace_text_submitted));
  691. replace = memnew(Button);
  692. hbc_button_replace->add_child(replace);
  693. replace->set_text(TTR("Replace"));
  694. replace->connect(SceneStringName(pressed), callable_mp(this, &FindReplaceBar::_replace));
  695. replace_all = memnew(Button);
  696. hbc_button_replace->add_child(replace_all);
  697. replace_all->set_text(TTR("Replace All"));
  698. replace_all->connect(SceneStringName(pressed), callable_mp(this, &FindReplaceBar::_replace_all));
  699. selection_only = memnew(CheckBox);
  700. hbc_option_replace->add_child(selection_only);
  701. selection_only->set_text(TTR("Selection Only"));
  702. selection_only->set_focus_mode(FOCUS_NONE);
  703. selection_only->connect(SceneStringName(toggled), callable_mp(this, &FindReplaceBar::_search_options_changed));
  704. hide_button = memnew(TextureButton);
  705. add_child(hide_button);
  706. hide_button->set_tooltip_text(TTR("Hide"));
  707. hide_button->set_accessibility_name(TTRC("Hide"));
  708. hide_button->set_focus_mode(FOCUS_NONE);
  709. hide_button->connect(SceneStringName(pressed), callable_mp(this, &FindReplaceBar::_hide_bar));
  710. hide_button->set_v_size_flags(SIZE_SHRINK_CENTER);
  711. }
  712. /*** CODE EDITOR ****/
  713. static constexpr float ZOOM_FACTOR_PRESETS[8] = { 0.5f, 0.75f, 0.9f, 1.0f, 1.1f, 1.25f, 1.5f, 2.0f };
  714. // This function should be used to handle shortcuts that could otherwise
  715. // be handled too late if they weren't handled here.
  716. void CodeTextEditor::input(const Ref<InputEvent> &event) {
  717. ERR_FAIL_COND(event.is_null());
  718. const Ref<InputEventKey> key_event = event;
  719. if (key_event.is_null()) {
  720. return;
  721. }
  722. if (!key_event->is_pressed()) {
  723. return;
  724. }
  725. if (!text_editor->has_focus()) {
  726. 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())))) {
  727. if (ED_IS_SHORTCUT("script_text_editor/find_next", key_event)) {
  728. find_replace_bar->search_next();
  729. accept_event();
  730. return;
  731. }
  732. if (ED_IS_SHORTCUT("script_text_editor/find_previous", key_event)) {
  733. find_replace_bar->search_prev();
  734. accept_event();
  735. return;
  736. }
  737. }
  738. return;
  739. }
  740. if (ED_IS_SHORTCUT("script_text_editor/move_up", key_event)) {
  741. text_editor->move_lines_up();
  742. accept_event();
  743. return;
  744. }
  745. if (ED_IS_SHORTCUT("script_text_editor/move_down", key_event)) {
  746. text_editor->move_lines_down();
  747. accept_event();
  748. return;
  749. }
  750. if (ED_IS_SHORTCUT("script_text_editor/delete_line", key_event)) {
  751. text_editor->delete_lines();
  752. accept_event();
  753. return;
  754. }
  755. if (ED_IS_SHORTCUT("script_text_editor/duplicate_selection", key_event)) {
  756. text_editor->duplicate_selection();
  757. accept_event();
  758. return;
  759. }
  760. if (ED_IS_SHORTCUT("script_text_editor/duplicate_lines", key_event)) {
  761. text_editor->duplicate_lines();
  762. accept_event();
  763. return;
  764. }
  765. }
  766. void CodeTextEditor::_text_editor_gui_input(const Ref<InputEvent> &p_event) {
  767. Ref<InputEventMouseButton> mb = p_event;
  768. if (mb.is_valid()) {
  769. if (mb->is_pressed() && mb->is_command_or_control_pressed()) {
  770. if (mb->get_button_index() == MouseButton::WHEEL_UP) {
  771. _zoom_in();
  772. accept_event();
  773. return;
  774. }
  775. if (mb->get_button_index() == MouseButton::WHEEL_DOWN) {
  776. _zoom_out();
  777. accept_event();
  778. return;
  779. }
  780. }
  781. }
  782. #ifndef ANDROID_ENABLED
  783. Ref<InputEventMagnifyGesture> magnify_gesture = p_event;
  784. if (magnify_gesture.is_valid()) {
  785. _zoom_to(zoom_factor * powf(magnify_gesture->get_factor(), 0.25f));
  786. accept_event();
  787. return;
  788. }
  789. #endif
  790. Ref<InputEventKey> k = p_event;
  791. if (k.is_valid()) {
  792. if (k->is_pressed()) {
  793. if (ED_IS_SHORTCUT("script_editor/zoom_in", p_event)) {
  794. _zoom_in();
  795. accept_event();
  796. return;
  797. }
  798. if (ED_IS_SHORTCUT("script_editor/zoom_out", p_event)) {
  799. _zoom_out();
  800. accept_event();
  801. return;
  802. }
  803. if (ED_IS_SHORTCUT("script_editor/reset_zoom", p_event)) {
  804. _zoom_to(1);
  805. accept_event();
  806. return;
  807. }
  808. }
  809. }
  810. }
  811. void CodeTextEditor::_line_col_changed() {
  812. if (!code_complete_timer->is_stopped() && code_complete_timer_line != text_editor->get_caret_line()) {
  813. code_complete_timer->stop();
  814. }
  815. String line = text_editor->get_line(text_editor->get_caret_line());
  816. int positional_column = 0;
  817. for (int i = 0; i < text_editor->get_caret_column(); i++) {
  818. if (line[i] == '\t') {
  819. positional_column += text_editor->get_indent_size(); // Tab size
  820. } else {
  821. positional_column += 1;
  822. }
  823. }
  824. StringBuilder sb;
  825. sb.append(itos(text_editor->get_caret_line() + 1).lpad(4));
  826. sb.append(" : ");
  827. sb.append(itos(positional_column + 1).lpad(3));
  828. line_and_col_txt->set_text(sb.as_string());
  829. if (find_replace_bar) {
  830. if (!find_replace_bar->line_col_changed_for_result) {
  831. find_replace_bar->needs_to_count_results = true;
  832. }
  833. find_replace_bar->line_col_changed_for_result = false;
  834. }
  835. }
  836. void CodeTextEditor::_text_changed() {
  837. if (code_complete_enabled && text_editor->is_insert_text_operation()) {
  838. code_complete_timer_line = text_editor->get_caret_line();
  839. code_complete_timer->start();
  840. }
  841. idle->start();
  842. if (find_replace_bar) {
  843. find_replace_bar->needs_to_count_results = true;
  844. }
  845. }
  846. void CodeTextEditor::_code_complete_timer_timeout() {
  847. if (!is_visible_in_tree()) {
  848. return;
  849. }
  850. text_editor->request_code_completion();
  851. }
  852. void CodeTextEditor::_complete_request() {
  853. List<ScriptLanguage::CodeCompletionOption> entries;
  854. String ctext = text_editor->get_text_for_code_completion();
  855. _code_complete_script(ctext, &entries);
  856. bool forced = false;
  857. if (code_complete_func) {
  858. code_complete_func(code_complete_ud, ctext, &entries, forced);
  859. }
  860. for (const ScriptLanguage::CodeCompletionOption &e : entries) {
  861. Color font_color = completion_font_color;
  862. if (!e.theme_color_name.is_empty() && EDITOR_GET("text_editor/completion/colorize_suggestions")) {
  863. font_color = get_theme_color(e.theme_color_name, SNAME("Editor"));
  864. } else if (e.insert_text.begins_with("\"") || e.insert_text.begins_with("\'")) {
  865. font_color = completion_string_color;
  866. } else if (e.insert_text.begins_with("##") || e.insert_text.begins_with("///")) {
  867. font_color = completion_doc_comment_color;
  868. } else if (e.insert_text.begins_with("&")) {
  869. font_color = completion_string_name_color;
  870. } else if (e.insert_text.begins_with("^")) {
  871. font_color = completion_node_path_color;
  872. } else if (e.insert_text.begins_with("#") || e.insert_text.begins_with("//")) {
  873. font_color = completion_comment_color;
  874. }
  875. 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);
  876. }
  877. text_editor->update_code_completion_options(forced);
  878. }
  879. Ref<Texture2D> CodeTextEditor::_get_completion_icon(const ScriptLanguage::CodeCompletionOption &p_option) {
  880. Ref<Texture2D> tex;
  881. switch (p_option.kind) {
  882. case ScriptLanguage::CODE_COMPLETION_KIND_CLASS: {
  883. if (has_theme_icon(p_option.display, EditorStringName(EditorIcons))) {
  884. tex = get_editor_theme_icon(p_option.display);
  885. } else {
  886. tex = EditorNode::get_singleton()->get_class_icon(p_option.display);
  887. if (tex.is_null()) {
  888. tex = get_editor_theme_icon(SNAME("Object"));
  889. }
  890. }
  891. } break;
  892. case ScriptLanguage::CODE_COMPLETION_KIND_ENUM:
  893. tex = get_editor_theme_icon(SNAME("Enum"));
  894. break;
  895. case ScriptLanguage::CODE_COMPLETION_KIND_FILE_PATH:
  896. tex = get_editor_theme_icon(SNAME("File"));
  897. break;
  898. case ScriptLanguage::CODE_COMPLETION_KIND_NODE_PATH:
  899. tex = get_editor_theme_icon(SNAME("NodePath"));
  900. break;
  901. case ScriptLanguage::CODE_COMPLETION_KIND_VARIABLE:
  902. tex = get_editor_theme_icon(SNAME("LocalVariable"));
  903. break;
  904. case ScriptLanguage::CODE_COMPLETION_KIND_CONSTANT:
  905. tex = get_editor_theme_icon(SNAME("MemberConstant"));
  906. break;
  907. case ScriptLanguage::CODE_COMPLETION_KIND_MEMBER:
  908. tex = get_editor_theme_icon(SNAME("MemberProperty"));
  909. break;
  910. case ScriptLanguage::CODE_COMPLETION_KIND_SIGNAL:
  911. tex = get_editor_theme_icon(SNAME("MemberSignal"));
  912. break;
  913. case ScriptLanguage::CODE_COMPLETION_KIND_FUNCTION:
  914. tex = get_editor_theme_icon(SNAME("MemberMethod"));
  915. break;
  916. case ScriptLanguage::CODE_COMPLETION_KIND_PLAIN_TEXT:
  917. tex = get_editor_theme_icon(SNAME("BoxMesh"));
  918. break;
  919. default:
  920. tex = get_editor_theme_icon(SNAME("String"));
  921. break;
  922. }
  923. return tex;
  924. }
  925. void CodeTextEditor::update_editor_settings() {
  926. // Theme: Highlighting
  927. completion_font_color = EDITOR_GET("text_editor/theme/highlighting/completion_font_color");
  928. completion_string_color = EDITOR_GET("text_editor/theme/highlighting/string_color");
  929. completion_string_name_color = EDITOR_GET("text_editor/theme/highlighting/gdscript/string_name_color");
  930. completion_node_path_color = EDITOR_GET("text_editor/theme/highlighting/gdscript/node_path_color");
  931. completion_comment_color = EDITOR_GET("text_editor/theme/highlighting/comment_color");
  932. completion_doc_comment_color = EDITOR_GET("text_editor/theme/highlighting/doc_comment_color");
  933. // Appearance: Caret
  934. text_editor->set_caret_type((TextEdit::CaretType)EDITOR_GET("text_editor/appearance/caret/type").operator int());
  935. text_editor->set_caret_blink_enabled(EDITOR_GET("text_editor/appearance/caret/caret_blink"));
  936. text_editor->set_caret_blink_interval(EDITOR_GET("text_editor/appearance/caret/caret_blink_interval"));
  937. text_editor->set_highlight_current_line(EDITOR_GET("text_editor/appearance/caret/highlight_current_line"));
  938. text_editor->set_highlight_all_occurrences(EDITOR_GET("text_editor/appearance/caret/highlight_all_occurrences"));
  939. // Appearance: Gutters
  940. text_editor->set_draw_line_numbers(EDITOR_GET("text_editor/appearance/gutters/show_line_numbers"));
  941. text_editor->set_line_numbers_zero_padded(EDITOR_GET("text_editor/appearance/gutters/line_numbers_zero_padded"));
  942. // Appearance: Minimap
  943. text_editor->set_draw_minimap(EDITOR_GET("text_editor/appearance/minimap/show_minimap"));
  944. text_editor->set_minimap_width((int)EDITOR_GET("text_editor/appearance/minimap/minimap_width") * EDSCALE);
  945. // Appearance: Lines
  946. text_editor->set_line_folding_enabled(EDITOR_GET("text_editor/appearance/lines/code_folding"));
  947. text_editor->set_draw_fold_gutter(EDITOR_GET("text_editor/appearance/lines/code_folding"));
  948. text_editor->set_line_wrapping_mode((TextEdit::LineWrappingMode)EDITOR_GET("text_editor/appearance/lines/word_wrap").operator int());
  949. text_editor->set_autowrap_mode((TextServer::AutowrapMode)EDITOR_GET("text_editor/appearance/lines/autowrap_mode").operator int());
  950. // Appearance: Whitespace
  951. text_editor->set_draw_tabs(EDITOR_GET("text_editor/appearance/whitespace/draw_tabs"));
  952. text_editor->set_draw_spaces(EDITOR_GET("text_editor/appearance/whitespace/draw_spaces"));
  953. text_editor->add_theme_constant_override("line_spacing", EDITOR_GET("text_editor/appearance/whitespace/line_spacing"));
  954. // Behavior: General
  955. text_editor->set_empty_selection_clipboard_enabled(EDITOR_GET("text_editor/behavior/general/empty_selection_clipboard"));
  956. // Behavior: Navigation
  957. text_editor->set_scroll_past_end_of_file_enabled(EDITOR_GET("text_editor/behavior/navigation/scroll_past_end_of_file"));
  958. text_editor->set_smooth_scroll_enabled(EDITOR_GET("text_editor/behavior/navigation/smooth_scrolling"));
  959. text_editor->set_v_scroll_speed(EDITOR_GET("text_editor/behavior/navigation/v_scroll_speed"));
  960. text_editor->set_drag_and_drop_selection_enabled(EDITOR_GET("text_editor/behavior/navigation/drag_and_drop_selection"));
  961. text_editor->set_use_default_word_separators(EDITOR_GET("text_editor/behavior/navigation/use_default_word_separators"));
  962. text_editor->set_use_custom_word_separators(EDITOR_GET("text_editor/behavior/navigation/use_custom_word_separators"));
  963. text_editor->set_custom_word_separators(EDITOR_GET("text_editor/behavior/navigation/custom_word_separators"));
  964. // Behavior: Indent
  965. set_indent_using_spaces(EDITOR_GET("text_editor/behavior/indent/type"));
  966. text_editor->set_indent_size(EDITOR_GET("text_editor/behavior/indent/size"));
  967. text_editor->set_auto_indent_enabled(EDITOR_GET("text_editor/behavior/indent/auto_indent"));
  968. text_editor->set_indent_wrapped_lines(EDITOR_GET("text_editor/behavior/indent/indent_wrapped_lines"));
  969. // Completion
  970. text_editor->set_auto_brace_completion_enabled(EDITOR_GET("text_editor/completion/auto_brace_complete"));
  971. text_editor->set_code_hint_draw_below(EDITOR_GET("text_editor/completion/put_callhint_tooltip_below_current_line"));
  972. code_complete_enabled = EDITOR_GET("text_editor/completion/code_complete_enabled");
  973. code_complete_timer->set_wait_time(EDITOR_GET("text_editor/completion/code_complete_delay"));
  974. idle_time = EDITOR_GET("text_editor/completion/idle_parse_delay");
  975. idle_time_with_errors = EDITOR_GET("text_editor/completion/idle_parse_delay_with_errors_found");
  976. // Appearance: Guidelines
  977. if (EDITOR_GET("text_editor/appearance/guidelines/show_line_length_guidelines")) {
  978. TypedArray<int> guideline_cols;
  979. guideline_cols.append(EDITOR_GET("text_editor/appearance/guidelines/line_length_guideline_hard_column"));
  980. if (EDITOR_GET("text_editor/appearance/guidelines/line_length_guideline_soft_column") != guideline_cols[0]) {
  981. guideline_cols.append(EDITOR_GET("text_editor/appearance/guidelines/line_length_guideline_soft_column"));
  982. }
  983. text_editor->set_line_length_guidelines(guideline_cols);
  984. } else {
  985. text_editor->set_line_length_guidelines(TypedArray<int>());
  986. }
  987. set_zoom_factor(zoom_factor);
  988. }
  989. void CodeTextEditor::set_find_replace_bar(FindReplaceBar *p_bar) {
  990. if (find_replace_bar) {
  991. return;
  992. }
  993. find_replace_bar = p_bar;
  994. find_replace_bar->set_text_edit(this);
  995. find_replace_bar->connect("error", callable_mp(error, &Label::set_text));
  996. }
  997. void CodeTextEditor::remove_find_replace_bar() {
  998. if (!find_replace_bar) {
  999. return;
  1000. }
  1001. find_replace_bar->disconnect("error", callable_mp(error, &Label::set_text));
  1002. find_replace_bar = nullptr;
  1003. }
  1004. void CodeTextEditor::trim_trailing_whitespace() {
  1005. bool trimmed_whitespace = false;
  1006. for (int i = 0; i < text_editor->get_line_count(); i++) {
  1007. String line = text_editor->get_line(i);
  1008. if (line.ends_with(" ") || line.ends_with("\t")) {
  1009. if (!trimmed_whitespace) {
  1010. text_editor->begin_complex_operation();
  1011. trimmed_whitespace = true;
  1012. }
  1013. int end = 0;
  1014. for (int j = line.length() - 1; j > -1; j--) {
  1015. if (line[j] != ' ' && line[j] != '\t') {
  1016. end = j + 1;
  1017. break;
  1018. }
  1019. }
  1020. text_editor->remove_text(i, end, i, line.length());
  1021. }
  1022. }
  1023. if (trimmed_whitespace) {
  1024. text_editor->merge_overlapping_carets();
  1025. text_editor->end_complex_operation();
  1026. }
  1027. }
  1028. void CodeTextEditor::trim_final_newlines() {
  1029. int final_line = text_editor->get_line_count() - 1;
  1030. int check_line = final_line;
  1031. String line = text_editor->get_line(check_line);
  1032. while (line.is_empty() && check_line > -1) {
  1033. --check_line;
  1034. line = text_editor->get_line(check_line);
  1035. }
  1036. ++check_line;
  1037. if (check_line < final_line) {
  1038. text_editor->begin_complex_operation();
  1039. text_editor->remove_text(check_line, 0, final_line, 0);
  1040. text_editor->merge_overlapping_carets();
  1041. text_editor->end_complex_operation();
  1042. text_editor->queue_redraw();
  1043. }
  1044. }
  1045. void CodeTextEditor::insert_final_newline() {
  1046. int final_line = text_editor->get_line_count() - 1;
  1047. String line = text_editor->get_line(final_line);
  1048. // Length 0 means it's already an empty line, no need to add a newline.
  1049. if (line.length() > 0 && !line.ends_with("\n")) {
  1050. text_editor->insert_text("\n", final_line, line.length(), false);
  1051. }
  1052. }
  1053. void CodeTextEditor::convert_case(CaseStyle p_case) {
  1054. if (!text_editor->has_selection()) {
  1055. return;
  1056. }
  1057. text_editor->begin_complex_operation();
  1058. text_editor->begin_multicaret_edit();
  1059. for (int c = 0; c < text_editor->get_caret_count(); c++) {
  1060. if (text_editor->multicaret_edit_ignore_caret(c)) {
  1061. continue;
  1062. }
  1063. if (!text_editor->has_selection(c)) {
  1064. continue;
  1065. }
  1066. int begin = text_editor->get_selection_from_line(c);
  1067. int end = text_editor->get_selection_to_line(c);
  1068. int begin_col = text_editor->get_selection_from_column(c);
  1069. int end_col = text_editor->get_selection_to_column(c);
  1070. for (int i = begin; i <= end; i++) {
  1071. int len = text_editor->get_line(i).length();
  1072. if (i == end) {
  1073. len = end_col;
  1074. }
  1075. if (i == begin) {
  1076. len -= begin_col;
  1077. }
  1078. String new_line = text_editor->get_line(i).substr(i == begin ? begin_col : 0, len);
  1079. switch (p_case) {
  1080. case UPPER: {
  1081. new_line = new_line.to_upper();
  1082. } break;
  1083. case LOWER: {
  1084. new_line = new_line.to_lower();
  1085. } break;
  1086. case CAPITALIZE: {
  1087. new_line = new_line.capitalize();
  1088. } break;
  1089. }
  1090. if (i == begin) {
  1091. new_line = text_editor->get_line(i).left(begin_col) + new_line;
  1092. }
  1093. if (i == end) {
  1094. new_line = new_line + text_editor->get_line(i).substr(end_col);
  1095. }
  1096. text_editor->set_line(i, new_line);
  1097. }
  1098. }
  1099. text_editor->end_multicaret_edit();
  1100. text_editor->end_complex_operation();
  1101. }
  1102. void CodeTextEditor::set_indent_using_spaces(bool p_use_spaces) {
  1103. text_editor->set_indent_using_spaces(p_use_spaces);
  1104. indentation_txt->set_text(p_use_spaces ? TTR("Spaces", "Indentation") : TTR("Tabs", "Indentation"));
  1105. }
  1106. void CodeTextEditor::toggle_inline_comment(const String &delimiter) {
  1107. text_editor->begin_complex_operation();
  1108. text_editor->begin_multicaret_edit();
  1109. Vector<Point2i> line_ranges = text_editor->get_line_ranges_from_carets();
  1110. int folded_to = 0;
  1111. for (Point2i line_range : line_ranges) {
  1112. int from_line = line_range.x;
  1113. int to_line = line_range.y;
  1114. // If last line is folded, extends to the end of the folded section
  1115. if (text_editor->is_line_folded(to_line)) {
  1116. folded_to = text_editor->get_next_visible_line_offset_from(to_line + 1, 1) - 1;
  1117. to_line += folded_to;
  1118. }
  1119. // Check first if there's any uncommented lines in selection.
  1120. bool is_commented = true;
  1121. bool is_all_empty = true;
  1122. for (int line = from_line; line <= to_line; line++) {
  1123. // `+ delimiter.length()` here because comment delimiter is not actually `in comment` so we check first character after it
  1124. int delimiter_idx = text_editor->is_in_comment(line, text_editor->get_first_non_whitespace_column(line) + delimiter.length());
  1125. // Empty lines should not be counted.
  1126. bool is_empty = text_editor->get_line(line).strip_edges().is_empty();
  1127. is_all_empty = is_all_empty && is_empty;
  1128. // get_delimiter_start_key will return `##` instead of `#` when there is multiple comment delimiter in a line.
  1129. if (!is_empty && (delimiter_idx == -1 || !text_editor->get_delimiter_start_key(delimiter_idx).begins_with(delimiter))) {
  1130. is_commented = false;
  1131. break;
  1132. }
  1133. }
  1134. // Special case for commenting empty lines, treat it/them as uncommented lines.
  1135. is_commented = is_commented && !is_all_empty;
  1136. // Comment/uncomment.
  1137. for (int line = from_line; line <= to_line; line++) {
  1138. if (is_all_empty) {
  1139. text_editor->insert_text(delimiter, line, 0);
  1140. continue;
  1141. }
  1142. if (is_commented) {
  1143. int delimiter_column = text_editor->get_line(line).find(delimiter);
  1144. text_editor->remove_text(line, delimiter_column, line, delimiter_column + delimiter.length());
  1145. } else {
  1146. text_editor->insert_text(delimiter, line, text_editor->get_first_non_whitespace_column(line));
  1147. }
  1148. }
  1149. }
  1150. text_editor->end_multicaret_edit();
  1151. text_editor->end_complex_operation();
  1152. }
  1153. void CodeTextEditor::goto_line(int p_line, int p_column) {
  1154. text_editor->remove_secondary_carets();
  1155. text_editor->deselect();
  1156. text_editor->unfold_line(CLAMP(p_line, 0, text_editor->get_line_count() - 1));
  1157. text_editor->set_caret_line(p_line, false);
  1158. text_editor->set_caret_column(p_column, false);
  1159. text_editor->set_code_hint("");
  1160. text_editor->cancel_code_completion();
  1161. // Defer in case the CodeEdit was just created and needs to be resized.
  1162. callable_mp((TextEdit *)text_editor, &TextEdit::adjust_viewport_to_caret).call_deferred(0);
  1163. }
  1164. void CodeTextEditor::goto_line_selection(int p_line, int p_begin, int p_end) {
  1165. text_editor->remove_secondary_carets();
  1166. text_editor->unfold_line(CLAMP(p_line, 0, text_editor->get_line_count() - 1));
  1167. text_editor->select(p_line, p_begin, p_line, p_end);
  1168. text_editor->set_code_hint("");
  1169. text_editor->cancel_code_completion();
  1170. callable_mp((TextEdit *)text_editor, &TextEdit::adjust_viewport_to_caret).call_deferred(0);
  1171. }
  1172. void CodeTextEditor::goto_line_centered(int p_line, int p_column) {
  1173. text_editor->remove_secondary_carets();
  1174. text_editor->deselect();
  1175. text_editor->unfold_line(CLAMP(p_line, 0, text_editor->get_line_count() - 1));
  1176. text_editor->set_caret_line(p_line, false);
  1177. text_editor->set_caret_column(p_column, false);
  1178. text_editor->set_code_hint("");
  1179. text_editor->cancel_code_completion();
  1180. callable_mp((TextEdit *)text_editor, &TextEdit::center_viewport_to_caret).call_deferred(0);
  1181. }
  1182. void CodeTextEditor::set_executing_line(int p_line) {
  1183. text_editor->set_line_as_executing(p_line, true);
  1184. }
  1185. void CodeTextEditor::clear_executing_line() {
  1186. text_editor->clear_executing_lines();
  1187. }
  1188. Variant CodeTextEditor::get_edit_state() {
  1189. Dictionary state;
  1190. state.merge(get_navigation_state());
  1191. state["folded_lines"] = text_editor->get_folded_lines();
  1192. state["breakpoints"] = text_editor->get_breakpointed_lines();
  1193. state["bookmarks"] = text_editor->get_bookmarked_lines();
  1194. Ref<EditorSyntaxHighlighter> syntax_highlighter = text_editor->get_syntax_highlighter();
  1195. state["syntax_highlighter"] = syntax_highlighter->_get_name();
  1196. return state;
  1197. }
  1198. Variant CodeTextEditor::get_previous_state() {
  1199. return previous_state;
  1200. }
  1201. void CodeTextEditor::store_previous_state() {
  1202. previous_state = get_navigation_state();
  1203. }
  1204. bool CodeTextEditor::is_previewing_navigation_change() const {
  1205. return preview_navigation_change;
  1206. }
  1207. void CodeTextEditor::set_preview_navigation_change(bool p_preview) {
  1208. if (preview_navigation_change == p_preview) {
  1209. return;
  1210. }
  1211. preview_navigation_change = p_preview;
  1212. if (!preview_navigation_change) {
  1213. emit_signal("navigation_preview_ended");
  1214. }
  1215. }
  1216. void CodeTextEditor::set_edit_state(const Variant &p_state) {
  1217. Dictionary state = p_state;
  1218. /* update the row first as it sets the column to 0 */
  1219. text_editor->set_caret_line(state["row"]);
  1220. text_editor->set_caret_column(state["column"]);
  1221. if (int(state["scroll_position"]) == -1) {
  1222. // Special case for previous state.
  1223. text_editor->center_viewport_to_caret();
  1224. } else {
  1225. text_editor->set_v_scroll(state["scroll_position"]);
  1226. }
  1227. text_editor->set_h_scroll(state["h_scroll_position"]);
  1228. if (state.get("selection", false)) {
  1229. text_editor->select(state["selection_from_line"], state["selection_from_column"], state["selection_to_line"], state["selection_to_column"]);
  1230. } else {
  1231. text_editor->deselect();
  1232. }
  1233. if (state.has("folded_lines")) {
  1234. Vector<int> folded_lines = state["folded_lines"];
  1235. for (int i = 0; i < folded_lines.size(); i++) {
  1236. text_editor->fold_line(folded_lines[i]);
  1237. }
  1238. }
  1239. if (state.has("breakpoints")) {
  1240. Array breakpoints = state["breakpoints"];
  1241. for (int i = 0; i < breakpoints.size(); i++) {
  1242. text_editor->set_line_as_breakpoint(breakpoints[i], true);
  1243. }
  1244. }
  1245. if (state.has("bookmarks")) {
  1246. Array bookmarks = state["bookmarks"];
  1247. for (int i = 0; i < bookmarks.size(); i++) {
  1248. text_editor->set_line_as_bookmarked(bookmarks[i], true);
  1249. }
  1250. }
  1251. if (previous_state.is_empty()) {
  1252. previous_state = p_state;
  1253. }
  1254. }
  1255. Variant CodeTextEditor::get_navigation_state() {
  1256. Dictionary state;
  1257. state["scroll_position"] = text_editor->get_v_scroll();
  1258. state["h_scroll_position"] = text_editor->get_h_scroll();
  1259. state["column"] = text_editor->get_caret_column();
  1260. state["row"] = text_editor->get_caret_line();
  1261. state["selection"] = get_text_editor()->has_selection();
  1262. if (get_text_editor()->has_selection()) {
  1263. state["selection_from_line"] = text_editor->get_selection_from_line();
  1264. state["selection_from_column"] = text_editor->get_selection_from_column();
  1265. state["selection_to_line"] = text_editor->get_selection_to_line();
  1266. state["selection_to_column"] = text_editor->get_selection_to_column();
  1267. }
  1268. return state;
  1269. }
  1270. void CodeTextEditor::set_error(const String &p_error) {
  1271. error->set_text(p_error);
  1272. if (!p_error.is_empty()) {
  1273. error->set_default_cursor_shape(CURSOR_POINTING_HAND);
  1274. } else {
  1275. error->set_default_cursor_shape(CURSOR_ARROW);
  1276. }
  1277. }
  1278. void CodeTextEditor::set_error_pos(int p_line, int p_column) {
  1279. error_line = p_line;
  1280. error_column = p_column;
  1281. }
  1282. Point2i CodeTextEditor::get_error_pos() const {
  1283. return Point2i(error_line, error_column);
  1284. }
  1285. void CodeTextEditor::goto_error() {
  1286. if (!error->get_text().is_empty()) {
  1287. int corrected_column = error_column;
  1288. const String line_text = text_editor->get_line(error_line);
  1289. const int indent_size = text_editor->get_indent_size();
  1290. if (indent_size > 1) {
  1291. const int tab_count = line_text.length() - line_text.lstrip("\t").length();
  1292. corrected_column -= tab_count * (indent_size - 1);
  1293. }
  1294. goto_line_centered(error_line, corrected_column);
  1295. }
  1296. }
  1297. void CodeTextEditor::_update_text_editor_theme() {
  1298. emit_signal(SNAME("load_theme_settings"));
  1299. error_button->set_button_icon(get_editor_theme_icon(SNAME("StatusError")));
  1300. warning_button->set_button_icon(get_editor_theme_icon(SNAME("NodeWarning")));
  1301. Ref<Font> status_bar_font = get_theme_font(SNAME("status_source"), EditorStringName(EditorFonts));
  1302. int status_bar_font_size = get_theme_font_size(SNAME("status_source_size"), EditorStringName(EditorFonts));
  1303. int count = status_bar->get_child_count();
  1304. for (int i = 0; i < count; i++) {
  1305. Control *n = Object::cast_to<Control>(status_bar->get_child(i));
  1306. if (n) {
  1307. n->add_theme_font_override(SceneStringName(font), status_bar_font);
  1308. n->add_theme_font_size_override(SceneStringName(font_size), status_bar_font_size);
  1309. }
  1310. }
  1311. const Color &error_color = get_theme_color(SNAME("error_color"), EditorStringName(Editor));
  1312. const Color &warning_color = get_theme_color(SNAME("warning_color"), EditorStringName(Editor));
  1313. error->add_theme_color_override(SceneStringName(font_color), error_color);
  1314. error_button->add_theme_color_override(SceneStringName(font_color), error_color);
  1315. warning_button->add_theme_color_override(SceneStringName(font_color), warning_color);
  1316. _update_font_ligatures();
  1317. }
  1318. void CodeTextEditor::_update_font_ligatures() {
  1319. int ot_mode = EDITOR_GET("interface/editor/code_font_contextual_ligatures");
  1320. Ref<FontVariation> fc = text_editor->get_theme_font(SceneStringName(font));
  1321. if (fc.is_valid()) {
  1322. switch (ot_mode) {
  1323. case 1: { // Disable ligatures.
  1324. Dictionary ftrs;
  1325. ftrs[TS->name_to_tag("calt")] = 0;
  1326. fc->set_opentype_features(ftrs);
  1327. } break;
  1328. case 2: { // Custom.
  1329. Vector<String> subtag = String(EDITOR_GET("interface/editor/code_font_custom_opentype_features")).split(",");
  1330. Dictionary ftrs;
  1331. for (int i = 0; i < subtag.size(); i++) {
  1332. Vector<String> subtag_a = subtag[i].split("=");
  1333. if (subtag_a.size() == 2) {
  1334. ftrs[TS->name_to_tag(subtag_a[0])] = subtag_a[1].to_int();
  1335. } else if (subtag_a.size() == 1) {
  1336. ftrs[TS->name_to_tag(subtag_a[0])] = 1;
  1337. }
  1338. }
  1339. fc->set_opentype_features(ftrs);
  1340. } break;
  1341. default: { // Enabled.
  1342. Dictionary ftrs;
  1343. ftrs[TS->name_to_tag("calt")] = 1;
  1344. fc->set_opentype_features(ftrs);
  1345. } break;
  1346. }
  1347. }
  1348. }
  1349. void CodeTextEditor::_text_changed_idle_timeout() {
  1350. _validate_script();
  1351. emit_signal(SNAME("validate_script"));
  1352. }
  1353. void CodeTextEditor::validate_script() {
  1354. idle->start();
  1355. }
  1356. void CodeTextEditor::_error_button_pressed() {
  1357. _set_show_errors_panel(!is_errors_panel_opened);
  1358. _set_show_warnings_panel(false);
  1359. }
  1360. void CodeTextEditor::_warning_button_pressed() {
  1361. _set_show_warnings_panel(!is_warnings_panel_opened);
  1362. _set_show_errors_panel(false);
  1363. }
  1364. void CodeTextEditor::_zoom_popup_id_pressed(int p_idx) {
  1365. _zoom_to(zoom_button->get_popup()->get_item_metadata(p_idx));
  1366. }
  1367. void CodeTextEditor::_set_show_errors_panel(bool p_show) {
  1368. is_errors_panel_opened = p_show;
  1369. emit_signal(SNAME("show_errors_panel"), p_show);
  1370. }
  1371. void CodeTextEditor::_set_show_warnings_panel(bool p_show) {
  1372. is_warnings_panel_opened = p_show;
  1373. emit_signal(SNAME("show_warnings_panel"), p_show);
  1374. }
  1375. void CodeTextEditor::_toggle_files_pressed() {
  1376. ERR_FAIL_NULL(toggle_files_list);
  1377. toggle_files_list->set_visible(!toggle_files_list->is_visible());
  1378. update_toggle_files_button();
  1379. }
  1380. void CodeTextEditor::_error_pressed(const Ref<InputEvent> &p_event) {
  1381. Ref<InputEventMouseButton> mb = p_event;
  1382. if (mb.is_valid() && mb->is_pressed() && mb->get_button_index() == MouseButton::LEFT) {
  1383. goto_error();
  1384. }
  1385. }
  1386. void CodeTextEditor::_notification(int p_what) {
  1387. switch (p_what) {
  1388. case NOTIFICATION_READY: {
  1389. set_error_count(0);
  1390. set_warning_count(0);
  1391. } break;
  1392. case NOTIFICATION_THEME_CHANGED: {
  1393. if (toggle_files_button->is_visible()) {
  1394. update_toggle_files_button();
  1395. }
  1396. _update_text_editor_theme();
  1397. } break;
  1398. case NOTIFICATION_LAYOUT_DIRECTION_CHANGED:
  1399. case NOTIFICATION_TRANSLATION_CHANGED: {
  1400. if (toggle_files_button->is_visible()) {
  1401. update_toggle_files_button();
  1402. }
  1403. } break;
  1404. case NOTIFICATION_VISIBILITY_CHANGED: {
  1405. if (toggle_files_button->is_visible()) {
  1406. update_toggle_files_button();
  1407. }
  1408. set_process_input(is_visible_in_tree());
  1409. } break;
  1410. case NOTIFICATION_PREDELETE: {
  1411. if (find_replace_bar) {
  1412. find_replace_bar->set_text_edit(nullptr);
  1413. }
  1414. } break;
  1415. }
  1416. }
  1417. void CodeTextEditor::set_error_count(int p_error_count) {
  1418. error_button->set_text(itos(p_error_count));
  1419. error_button->set_visible(p_error_count > 0);
  1420. if (p_error_count > 0) {
  1421. _set_show_errors_panel(false);
  1422. idle->set_wait_time(idle_time_with_errors); // Parsing should happen sooner.
  1423. } else {
  1424. idle->set_wait_time(idle_time);
  1425. }
  1426. }
  1427. void CodeTextEditor::set_warning_count(int p_warning_count) {
  1428. warning_button->set_text(itos(p_warning_count));
  1429. warning_button->set_visible(p_warning_count > 0);
  1430. if (!p_warning_count) {
  1431. _set_show_warnings_panel(false);
  1432. }
  1433. }
  1434. void CodeTextEditor::toggle_bookmark() {
  1435. Vector<int> sorted_carets = text_editor->get_sorted_carets();
  1436. int last_line = -1;
  1437. for (const int &c : sorted_carets) {
  1438. int from = text_editor->get_selection_from_line(c);
  1439. from += from == last_line ? 1 : 0;
  1440. int to = text_editor->get_selection_to_line(c);
  1441. if (to < from) {
  1442. continue;
  1443. }
  1444. // Check first if there's any bookmarked lines in the selection.
  1445. bool selection_has_bookmarks = false;
  1446. for (int line = from; line <= to; line++) {
  1447. if (text_editor->is_line_bookmarked(line)) {
  1448. selection_has_bookmarks = true;
  1449. break;
  1450. }
  1451. }
  1452. // Set bookmark on caret or remove all bookmarks from the selection.
  1453. if (!selection_has_bookmarks) {
  1454. if (text_editor->get_caret_line(c) != last_line) {
  1455. text_editor->set_line_as_bookmarked(text_editor->get_caret_line(c), true);
  1456. }
  1457. } else {
  1458. for (int line = from; line <= to; line++) {
  1459. text_editor->set_line_as_bookmarked(line, false);
  1460. }
  1461. }
  1462. last_line = to;
  1463. }
  1464. }
  1465. void CodeTextEditor::goto_next_bookmark() {
  1466. PackedInt32Array bmarks = text_editor->get_bookmarked_lines();
  1467. if (bmarks.is_empty()) {
  1468. return;
  1469. }
  1470. int current_line = text_editor->get_caret_line();
  1471. int bmark_idx = 0;
  1472. if (current_line < (int)bmarks[bmarks.size() - 1]) {
  1473. while (bmark_idx < bmarks.size() && bmarks[bmark_idx] <= current_line) {
  1474. bmark_idx++;
  1475. }
  1476. }
  1477. goto_line_centered(bmarks[bmark_idx]);
  1478. }
  1479. void CodeTextEditor::goto_prev_bookmark() {
  1480. PackedInt32Array bmarks = text_editor->get_bookmarked_lines();
  1481. if (bmarks.is_empty()) {
  1482. return;
  1483. }
  1484. int current_line = text_editor->get_caret_line();
  1485. int bmark_idx = bmarks.size() - 1;
  1486. if (current_line > (int)bmarks[0]) {
  1487. while (bmark_idx >= 0 && bmarks[bmark_idx] >= current_line) {
  1488. bmark_idx--;
  1489. }
  1490. }
  1491. goto_line_centered(bmarks[bmark_idx]);
  1492. }
  1493. void CodeTextEditor::remove_all_bookmarks() {
  1494. text_editor->clear_bookmarked_lines();
  1495. }
  1496. void CodeTextEditor::_zoom_in() {
  1497. int s = text_editor->get_theme_font_size(SceneStringName(font_size));
  1498. _zoom_to(zoom_factor * (s + MAX(1.0f, EDSCALE)) / s);
  1499. }
  1500. void CodeTextEditor::_zoom_out() {
  1501. int s = text_editor->get_theme_font_size(SceneStringName(font_size));
  1502. _zoom_to(zoom_factor * (s - MAX(1.0f, EDSCALE)) / s);
  1503. }
  1504. void CodeTextEditor::_zoom_to(float p_zoom_factor) {
  1505. if (zoom_factor == p_zoom_factor) {
  1506. return;
  1507. }
  1508. float old_zoom_factor = zoom_factor;
  1509. set_zoom_factor(p_zoom_factor);
  1510. if (old_zoom_factor != zoom_factor) {
  1511. emit_signal(SNAME("zoomed"), zoom_factor);
  1512. }
  1513. }
  1514. void CodeTextEditor::set_zoom_factor(float p_zoom_factor) {
  1515. zoom_factor = CLAMP(p_zoom_factor, 0.25f, 3.0f);
  1516. int neutral_font_size = int(EDITOR_GET("interface/editor/code_font_size")) * EDSCALE;
  1517. int new_font_size = Math::round(zoom_factor * neutral_font_size);
  1518. zoom_button->set_text(itos(Math::round(zoom_factor * 100)) + " %");
  1519. if (text_editor->has_theme_font_size_override(SceneStringName(font_size))) {
  1520. text_editor->remove_theme_font_size_override(SceneStringName(font_size));
  1521. }
  1522. text_editor->add_theme_font_size_override(SceneStringName(font_size), new_font_size);
  1523. }
  1524. float CodeTextEditor::get_zoom_factor() {
  1525. return zoom_factor;
  1526. }
  1527. void CodeTextEditor::_bind_methods() {
  1528. ADD_SIGNAL(MethodInfo("validate_script"));
  1529. ADD_SIGNAL(MethodInfo("load_theme_settings"));
  1530. ADD_SIGNAL(MethodInfo("show_errors_panel"));
  1531. ADD_SIGNAL(MethodInfo("show_warnings_panel"));
  1532. ADD_SIGNAL(MethodInfo("navigation_preview_ended"));
  1533. ADD_SIGNAL(MethodInfo("zoomed", PropertyInfo(Variant::FLOAT, "p_zoom_factor")));
  1534. }
  1535. void CodeTextEditor::set_code_complete_func(CodeTextEditorCodeCompleteFunc p_code_complete_func, void *p_ud) {
  1536. code_complete_func = p_code_complete_func;
  1537. code_complete_ud = p_ud;
  1538. }
  1539. void CodeTextEditor::set_toggle_list_control(Control *p_control) {
  1540. toggle_files_list = p_control;
  1541. }
  1542. void CodeTextEditor::show_toggle_files_button() {
  1543. toggle_files_button->show();
  1544. }
  1545. void CodeTextEditor::update_toggle_files_button() {
  1546. ERR_FAIL_NULL(toggle_files_list);
  1547. bool forward = toggle_files_list->is_visible() == is_layout_rtl();
  1548. toggle_files_button->set_button_icon(get_editor_theme_icon(forward ? SNAME("Forward") : SNAME("Back")));
  1549. toggle_files_button->set_tooltip_text(vformat("%s (%s)", TTR("Toggle Files Panel"), ED_GET_SHORTCUT("script_editor/toggle_files_panel")->get_as_text()));
  1550. }
  1551. CodeTextEditor::CodeTextEditor() {
  1552. code_complete_func = nullptr;
  1553. ED_SHORTCUT("script_editor/zoom_in", TTRC("Zoom In"), KeyModifierMask::CMD_OR_CTRL | Key::EQUAL);
  1554. ED_SHORTCUT("script_editor/zoom_out", TTRC("Zoom Out"), KeyModifierMask::CMD_OR_CTRL | Key::MINUS);
  1555. ED_SHORTCUT_ARRAY("script_editor/reset_zoom", TTRC("Reset Zoom"),
  1556. { int32_t(KeyModifierMask::CMD_OR_CTRL | Key::KEY_0), int32_t(KeyModifierMask::CMD_OR_CTRL | Key::KP_0) });
  1557. text_editor = memnew(CodeEdit);
  1558. add_child(text_editor);
  1559. text_editor->set_v_size_flags(SIZE_EXPAND_FILL);
  1560. text_editor->set_structured_text_bidi_override(TextServer::STRUCTURED_TEXT_GDSCRIPT);
  1561. text_editor->set_draw_bookmarks_gutter(true);
  1562. text_editor->set_draw_line_numbers(true);
  1563. text_editor->set_highlight_matching_braces_enabled(true);
  1564. text_editor->set_auto_indent_enabled(true);
  1565. text_editor->set_deselect_on_focus_loss_enabled(false);
  1566. status_bar = memnew(HBoxContainer);
  1567. add_child(status_bar);
  1568. status_bar->set_h_size_flags(SIZE_EXPAND_FILL);
  1569. status_bar->set_custom_minimum_size(Size2(0, 24 * EDSCALE)); // Adjust for the height of the warning icon.
  1570. idle = memnew(Timer);
  1571. add_child(idle);
  1572. idle->set_one_shot(true);
  1573. code_complete_enabled = EDITOR_GET("text_editor/completion/code_complete_enabled");
  1574. code_complete_timer = memnew(Timer);
  1575. add_child(code_complete_timer);
  1576. code_complete_timer->set_one_shot(true);
  1577. error_line = 0;
  1578. error_column = 0;
  1579. toggle_files_button = memnew(Button);
  1580. toggle_files_button->set_flat(true);
  1581. toggle_files_button->set_v_size_flags(SIZE_EXPAND | SIZE_SHRINK_CENTER);
  1582. toggle_files_button->connect(SceneStringName(pressed), callable_mp(this, &CodeTextEditor::_toggle_files_pressed));
  1583. toggle_files_button->set_accessibility_name(TTRC("Scripts"));
  1584. status_bar->add_child(toggle_files_button);
  1585. toggle_files_button->hide();
  1586. // Error
  1587. ScrollContainer *scroll = memnew(ScrollContainer);
  1588. scroll->set_h_size_flags(SIZE_EXPAND_FILL);
  1589. scroll->set_v_size_flags(SIZE_EXPAND_FILL);
  1590. scroll->set_vertical_scroll_mode(ScrollContainer::SCROLL_MODE_DISABLED);
  1591. status_bar->add_child(scroll);
  1592. error = memnew(Label);
  1593. error->set_focus_mode(FOCUS_ACCESSIBILITY);
  1594. scroll->add_child(error);
  1595. error->set_v_size_flags(SIZE_EXPAND | SIZE_SHRINK_CENTER);
  1596. error->set_mouse_filter(MOUSE_FILTER_STOP);
  1597. error->connect(SceneStringName(gui_input), callable_mp(this, &CodeTextEditor::_error_pressed));
  1598. // Errors
  1599. error_button = memnew(Button);
  1600. error_button->set_flat(true);
  1601. status_bar->add_child(error_button);
  1602. error_button->set_v_size_flags(SIZE_EXPAND | SIZE_SHRINK_CENTER);
  1603. error_button->set_default_cursor_shape(CURSOR_POINTING_HAND);
  1604. error_button->connect(SceneStringName(pressed), callable_mp(this, &CodeTextEditor::_error_button_pressed));
  1605. error_button->set_tooltip_text(TTR("Errors"));
  1606. error_button->set_accessibility_name(TTRC("Errors"));
  1607. // Warnings
  1608. warning_button = memnew(Button);
  1609. warning_button->set_flat(true);
  1610. status_bar->add_child(warning_button);
  1611. warning_button->set_v_size_flags(SIZE_EXPAND | SIZE_SHRINK_CENTER);
  1612. warning_button->set_default_cursor_shape(CURSOR_POINTING_HAND);
  1613. warning_button->connect(SceneStringName(pressed), callable_mp(this, &CodeTextEditor::_warning_button_pressed));
  1614. warning_button->set_tooltip_text(TTR("Warnings"));
  1615. warning_button->set_accessibility_name(TTRC("Warnings"));
  1616. status_bar->add_child(memnew(VSeparator));
  1617. // Zoom
  1618. zoom_button = memnew(MenuButton);
  1619. status_bar->add_child(zoom_button);
  1620. zoom_button->set_flat(true);
  1621. zoom_button->set_v_size_flags(SIZE_EXPAND | SIZE_SHRINK_CENTER);
  1622. zoom_button->set_tooltip_text(
  1623. TTR("Zoom factor") + "\n" +
  1624. // TRANSLATORS: The placeholders are keyboard shortcuts. The first one is in the form of "Ctrl+"/"Cmd+".
  1625. 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()));
  1626. zoom_button->set_text("100 %");
  1627. zoom_button->set_accessibility_name(TTRC("Zoom Factor"));
  1628. PopupMenu *zoom_menu = zoom_button->get_popup();
  1629. constexpr int preset_count = std::size(ZOOM_FACTOR_PRESETS);
  1630. for (int i = 0; i < preset_count; i++) {
  1631. float z = ZOOM_FACTOR_PRESETS[i];
  1632. zoom_menu->add_item(itos(Math::round(z * 100)) + " %");
  1633. zoom_menu->set_item_metadata(i, z);
  1634. }
  1635. zoom_menu->connect(SceneStringName(id_pressed), callable_mp(this, &CodeTextEditor::_zoom_popup_id_pressed));
  1636. status_bar->add_child(memnew(VSeparator));
  1637. // Line and column
  1638. line_and_col_txt = memnew(Label);
  1639. status_bar->add_child(line_and_col_txt);
  1640. line_and_col_txt->set_v_size_flags(SIZE_EXPAND | SIZE_SHRINK_CENTER);
  1641. line_and_col_txt->set_tooltip_text(TTR("Line and column numbers."));
  1642. line_and_col_txt->set_accessibility_name(TTRC("Line and Column Numbers"));
  1643. line_and_col_txt->set_focus_mode(FOCUS_ACCESSIBILITY);
  1644. line_and_col_txt->set_mouse_filter(MOUSE_FILTER_STOP);
  1645. status_bar->add_child(memnew(VSeparator));
  1646. // Indentation
  1647. indentation_txt = memnew(Label);
  1648. status_bar->add_child(indentation_txt);
  1649. indentation_txt->set_v_size_flags(SIZE_EXPAND | SIZE_SHRINK_CENTER);
  1650. indentation_txt->set_tooltip_text(TTR("Indentation"));
  1651. indentation_txt->set_accessibility_name(TTRC("Indentation"));
  1652. indentation_txt->set_focus_mode(FOCUS_ACCESSIBILITY);
  1653. indentation_txt->set_mouse_filter(MOUSE_FILTER_STOP);
  1654. text_editor->connect(SceneStringName(gui_input), callable_mp(this, &CodeTextEditor::_text_editor_gui_input));
  1655. text_editor->connect("caret_changed", callable_mp(this, &CodeTextEditor::_line_col_changed));
  1656. text_editor->connect(SceneStringName(text_changed), callable_mp(this, &CodeTextEditor::_text_changed));
  1657. text_editor->connect("code_completion_requested", callable_mp(this, &CodeTextEditor::_complete_request));
  1658. TypedArray<String> cs = { ".", ",", "(", "=", "$", "@", "\"", "\'" };
  1659. text_editor->set_code_completion_prefixes(cs);
  1660. idle->connect("timeout", callable_mp(this, &CodeTextEditor::_text_changed_idle_timeout));
  1661. code_complete_timer->connect("timeout", callable_mp(this, &CodeTextEditor::_code_complete_timer_timeout));
  1662. add_theme_constant_override("separation", 4 * EDSCALE);
  1663. }