code_editor.cpp 68 KB

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