code_editor.cpp 62 KB

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