code_editor.cpp 60 KB

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