code_editor.cpp 64 KB

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