code_editor.cpp 53 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743
  1. /*************************************************************************/
  2. /* code_editor.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 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/os/input.h"
  32. #include "core/os/keyboard.h"
  33. #include "core/string_builder.h"
  34. #include "editor/editor_scale.h"
  35. #include "editor_node.h"
  36. #include "editor_settings.h"
  37. #include "scene/gui/margin_container.h"
  38. #include "scene/gui/separator.h"
  39. #include "scene/resources/dynamic_font.h"
  40. void GotoLineDialog::popup_find_line(TextEdit *p_edit) {
  41. text_editor = p_edit;
  42. line->set_text(itos(text_editor->cursor_get_line()));
  43. line->select_all();
  44. popup_centered(Size2(180, 80) * EDSCALE);
  45. line->grab_focus();
  46. }
  47. int GotoLineDialog::get_line() const {
  48. return line->get_text().to_int();
  49. }
  50. void GotoLineDialog::ok_pressed() {
  51. if (get_line() < 1 || get_line() > text_editor->get_line_count())
  52. return;
  53. text_editor->unfold_line(get_line() - 1);
  54. text_editor->cursor_set_line(get_line() - 1);
  55. hide();
  56. }
  57. GotoLineDialog::GotoLineDialog() {
  58. set_title(TTR("Go to Line"));
  59. VBoxContainer *vbc = memnew(VBoxContainer);
  60. vbc->set_anchor_and_margin(MARGIN_LEFT, ANCHOR_BEGIN, 8 * EDSCALE);
  61. vbc->set_anchor_and_margin(MARGIN_TOP, ANCHOR_BEGIN, 8 * EDSCALE);
  62. vbc->set_anchor_and_margin(MARGIN_RIGHT, ANCHOR_END, -8 * EDSCALE);
  63. vbc->set_anchor_and_margin(MARGIN_BOTTOM, ANCHOR_END, -8 * EDSCALE);
  64. add_child(vbc);
  65. Label *l = memnew(Label);
  66. l->set_text(TTR("Line Number:"));
  67. vbc->add_child(l);
  68. line = memnew(LineEdit);
  69. vbc->add_child(line);
  70. register_text_enter(line);
  71. text_editor = NULL;
  72. set_hide_on_ok(false);
  73. }
  74. void FindReplaceBar::_notification(int p_what) {
  75. if (p_what == NOTIFICATION_READY) {
  76. find_prev->set_icon(get_icon("MoveUp", "EditorIcons"));
  77. find_next->set_icon(get_icon("MoveDown", "EditorIcons"));
  78. hide_button->set_normal_texture(get_icon("Close", "EditorIcons"));
  79. hide_button->set_hover_texture(get_icon("Close", "EditorIcons"));
  80. hide_button->set_pressed_texture(get_icon("Close", "EditorIcons"));
  81. hide_button->set_custom_minimum_size(hide_button->get_normal_texture()->get_size());
  82. } else if (p_what == NOTIFICATION_VISIBILITY_CHANGED) {
  83. set_process_unhandled_input(is_visible_in_tree());
  84. } else if (p_what == EditorSettings::NOTIFICATION_EDITOR_SETTINGS_CHANGED) {
  85. find_prev->set_icon(get_icon("MoveUp", "EditorIcons"));
  86. find_next->set_icon(get_icon("MoveDown", "EditorIcons"));
  87. hide_button->set_normal_texture(get_icon("Close", "EditorIcons"));
  88. hide_button->set_hover_texture(get_icon("Close", "EditorIcons"));
  89. hide_button->set_pressed_texture(get_icon("Close", "EditorIcons"));
  90. hide_button->set_custom_minimum_size(hide_button->get_normal_texture()->get_size());
  91. } else if (p_what == NOTIFICATION_THEME_CHANGED) {
  92. matches_label->add_color_override("font_color", results_count > 0 ? get_color("font_color", "Label") : get_color("error_color", "Editor"));
  93. }
  94. }
  95. void FindReplaceBar::_unhandled_input(const Ref<InputEvent> &p_event) {
  96. Ref<InputEventKey> k = p_event;
  97. if (k.is_valid()) {
  98. if (k->is_pressed() && (text_edit->has_focus() || vbc_lineedit->is_a_parent_of(get_focus_owner()))) {
  99. bool accepted = true;
  100. switch (k->get_scancode()) {
  101. case KEY_ESCAPE: {
  102. _hide_bar();
  103. } break;
  104. default: {
  105. accepted = false;
  106. } break;
  107. }
  108. if (accepted) {
  109. accept_event();
  110. }
  111. }
  112. }
  113. }
  114. bool FindReplaceBar::_search(uint32_t p_flags, int p_from_line, int p_from_col) {
  115. int line, col;
  116. String text = get_search_text();
  117. bool found = text_edit->search(text, p_flags, p_from_line, p_from_col, line, col);
  118. if (found) {
  119. if (!preserve_cursor) {
  120. text_edit->unfold_line(line);
  121. text_edit->cursor_set_line(line, false);
  122. text_edit->cursor_set_column(col + text.length(), false);
  123. text_edit->center_viewport_to_cursor();
  124. text_edit->select(line, col, line, col + text.length());
  125. }
  126. text_edit->set_search_text(text);
  127. text_edit->set_search_flags(p_flags);
  128. text_edit->set_current_search_result(line, col);
  129. result_line = line;
  130. result_col = col;
  131. _update_results_count();
  132. } else {
  133. results_count = 0;
  134. result_line = -1;
  135. result_col = -1;
  136. text_edit->set_search_text("");
  137. text_edit->set_search_flags(p_flags);
  138. text_edit->set_current_search_result(line, col);
  139. }
  140. _update_matches_label();
  141. return found;
  142. }
  143. void FindReplaceBar::_replace() {
  144. if (result_line != -1 && result_col != -1) {
  145. text_edit->begin_complex_operation();
  146. text_edit->unfold_line(result_line);
  147. text_edit->select(result_line, result_col, result_line, result_col + get_search_text().length());
  148. text_edit->insert_text_at_cursor(get_replace_text());
  149. text_edit->end_complex_operation();
  150. results_count = -1;
  151. }
  152. if (!search_current()) {
  153. search_next();
  154. }
  155. }
  156. void FindReplaceBar::_replace_all() {
  157. text_edit->disconnect("text_changed", this, "_editor_text_changed");
  158. // Line as x so it gets priority in comparison, column as y.
  159. Point2i orig_cursor(text_edit->cursor_get_line(), text_edit->cursor_get_column());
  160. Point2i prev_match = Point2(-1, -1);
  161. bool selection_enabled = text_edit->is_selection_active();
  162. Point2i selection_begin, selection_end;
  163. if (selection_enabled) {
  164. selection_begin = Point2i(text_edit->get_selection_from_line(), text_edit->get_selection_from_column());
  165. selection_end = Point2i(text_edit->get_selection_to_line(), text_edit->get_selection_to_column());
  166. }
  167. int vsval = text_edit->get_v_scroll();
  168. text_edit->cursor_set_line(0);
  169. text_edit->cursor_set_column(0);
  170. String replace_text = get_replace_text();
  171. int search_text_len = get_search_text().length();
  172. int rc = 0;
  173. replace_all_mode = true;
  174. text_edit->begin_complex_operation();
  175. if (search_current()) {
  176. do {
  177. // replace area
  178. Point2i match_from(result_line, result_col);
  179. Point2i match_to(result_line, result_col + search_text_len);
  180. if (match_from < prev_match) {
  181. break; // Done.
  182. }
  183. prev_match = Point2i(result_line, result_col + replace_text.length());
  184. text_edit->unfold_line(result_line);
  185. text_edit->select(result_line, result_col, result_line, match_to.y);
  186. if (selection_enabled && is_selection_only()) {
  187. if (match_from < selection_begin || match_to > selection_end) {
  188. continue;
  189. }
  190. // Replace but adjust selection bounds.
  191. text_edit->insert_text_at_cursor(replace_text);
  192. if (match_to.x == selection_end.x) {
  193. selection_end.y += replace_text.length() - search_text_len;
  194. }
  195. } else {
  196. // Just replace.
  197. text_edit->insert_text_at_cursor(replace_text);
  198. }
  199. rc++;
  200. } while (search_next());
  201. }
  202. text_edit->end_complex_operation();
  203. replace_all_mode = false;
  204. // Restore editor state (selection, cursor, scroll).
  205. text_edit->cursor_set_line(orig_cursor.x);
  206. text_edit->cursor_set_column(orig_cursor.y);
  207. if (selection_enabled && is_selection_only()) {
  208. // Reselect.
  209. text_edit->select(selection_begin.x, selection_begin.y, selection_end.x, selection_end.y);
  210. } else {
  211. text_edit->deselect();
  212. }
  213. text_edit->set_v_scroll(vsval);
  214. set_error(vformat(TTR("Replaced %d occurrence(s)."), rc));
  215. text_edit->call_deferred("connect", "text_changed", this, "_editor_text_changed");
  216. results_count = -1;
  217. }
  218. void FindReplaceBar::_get_search_from(int &r_line, int &r_col) {
  219. r_line = text_edit->cursor_get_line();
  220. r_col = text_edit->cursor_get_column();
  221. if (r_line == result_line && r_col >= result_col && r_col <= result_col + get_search_text().length()) {
  222. r_col = result_col;
  223. }
  224. }
  225. void FindReplaceBar::_update_results_count() {
  226. if (results_count != -1)
  227. return;
  228. results_count = 0;
  229. String searched = get_search_text();
  230. if (searched.empty()) return;
  231. String full_text = text_edit->get_text();
  232. int from_pos = 0;
  233. while (true) {
  234. int pos = is_case_sensitive() ? full_text.find(searched, from_pos) : full_text.findn(searched, from_pos);
  235. if (pos == -1) break;
  236. if (is_whole_words()) {
  237. from_pos++; // Making sure we won't hit the same match next time, if we get out via a continue.
  238. if (pos > 0 && !is_symbol(full_text[pos - 1]))
  239. continue;
  240. if (pos + searched.length() < full_text.length() && !is_symbol(full_text[pos + searched.length()]))
  241. continue;
  242. }
  243. results_count++;
  244. from_pos = pos + searched.length();
  245. }
  246. }
  247. void FindReplaceBar::_update_matches_label() {
  248. if (search_text->get_text().empty() || results_count == -1) {
  249. matches_label->hide();
  250. } else {
  251. matches_label->show();
  252. matches_label->add_color_override("font_color", results_count > 0 ? get_color("font_color", "Label") : get_color("error_color", "Editor"));
  253. matches_label->set_text(vformat(results_count == 1 ? TTR("%d match.") : TTR("%d matches."), results_count));
  254. }
  255. }
  256. bool FindReplaceBar::search_current() {
  257. uint32_t flags = 0;
  258. if (is_whole_words())
  259. flags |= TextEdit::SEARCH_WHOLE_WORDS;
  260. if (is_case_sensitive())
  261. flags |= TextEdit::SEARCH_MATCH_CASE;
  262. int line, col;
  263. _get_search_from(line, col);
  264. return _search(flags, line, col);
  265. }
  266. bool FindReplaceBar::search_prev() {
  267. if (!is_visible())
  268. popup_search(true);
  269. uint32_t flags = 0;
  270. String text = get_search_text();
  271. if (is_whole_words())
  272. flags |= TextEdit::SEARCH_WHOLE_WORDS;
  273. if (is_case_sensitive())
  274. flags |= TextEdit::SEARCH_MATCH_CASE;
  275. flags |= TextEdit::SEARCH_BACKWARDS;
  276. int line, col;
  277. _get_search_from(line, col);
  278. if (text_edit->is_selection_active())
  279. col--; // Skip currently selected word.
  280. col -= text.length();
  281. if (col < 0) {
  282. line -= 1;
  283. if (line < 0)
  284. line = text_edit->get_line_count() - 1;
  285. col = text_edit->get_line(line).length();
  286. }
  287. return _search(flags, line, col);
  288. }
  289. bool FindReplaceBar::search_next() {
  290. if (!is_visible())
  291. popup_search(true);
  292. uint32_t flags = 0;
  293. String text;
  294. if (replace_all_mode)
  295. text = get_replace_text();
  296. else
  297. text = get_search_text();
  298. if (is_whole_words())
  299. flags |= TextEdit::SEARCH_WHOLE_WORDS;
  300. if (is_case_sensitive())
  301. flags |= TextEdit::SEARCH_MATCH_CASE;
  302. int line, col;
  303. _get_search_from(line, col);
  304. if (line == result_line && col == result_col) {
  305. col += text.length();
  306. if (col > text_edit->get_line(line).length()) {
  307. line += 1;
  308. if (line >= text_edit->get_line_count())
  309. line = 0;
  310. col = 0;
  311. }
  312. }
  313. return _search(flags, line, col);
  314. }
  315. void FindReplaceBar::_hide_bar() {
  316. if (replace_text->has_focus() || search_text->has_focus())
  317. text_edit->grab_focus();
  318. text_edit->set_search_text("");
  319. result_line = -1;
  320. result_col = -1;
  321. hide();
  322. }
  323. void FindReplaceBar::_show_search(bool p_focus_replace, bool p_show_only) {
  324. show();
  325. if (p_show_only)
  326. return;
  327. if (p_focus_replace) {
  328. search_text->deselect();
  329. replace_text->call_deferred("grab_focus");
  330. } else {
  331. replace_text->deselect();
  332. search_text->call_deferred("grab_focus");
  333. }
  334. if (text_edit->is_selection_active() && !selection_only->is_pressed()) {
  335. search_text->set_text(text_edit->get_selection_text());
  336. }
  337. if (!get_search_text().empty()) {
  338. if (p_focus_replace) {
  339. replace_text->select_all();
  340. replace_text->set_cursor_position(replace_text->get_text().length());
  341. } else {
  342. search_text->select_all();
  343. search_text->set_cursor_position(search_text->get_text().length());
  344. }
  345. results_count = -1;
  346. _update_results_count();
  347. _update_matches_label();
  348. }
  349. }
  350. void FindReplaceBar::popup_search(bool p_show_only) {
  351. replace_text->hide();
  352. hbc_button_replace->hide();
  353. hbc_option_replace->hide();
  354. _show_search(false, p_show_only);
  355. }
  356. void FindReplaceBar::popup_replace() {
  357. if (!replace_text->is_visible_in_tree()) {
  358. replace_text->show();
  359. hbc_button_replace->show();
  360. hbc_option_replace->show();
  361. }
  362. selection_only->set_pressed((text_edit->is_selection_active() && text_edit->get_selection_from_line() < text_edit->get_selection_to_line()));
  363. _show_search(is_visible() || text_edit->is_selection_active());
  364. }
  365. void FindReplaceBar::_search_options_changed(bool p_pressed) {
  366. results_count = -1;
  367. search_current();
  368. }
  369. void FindReplaceBar::_editor_text_changed() {
  370. results_count = -1;
  371. if (is_visible_in_tree()) {
  372. preserve_cursor = true;
  373. search_current();
  374. preserve_cursor = false;
  375. }
  376. }
  377. void FindReplaceBar::_search_text_changed(const String &p_text) {
  378. results_count = -1;
  379. search_current();
  380. }
  381. void FindReplaceBar::_search_text_entered(const String &p_text) {
  382. if (Input::get_singleton()->is_key_pressed(KEY_SHIFT)) {
  383. search_prev();
  384. } else {
  385. search_next();
  386. }
  387. }
  388. void FindReplaceBar::_replace_text_entered(const String &p_text) {
  389. if (selection_only->is_pressed() && text_edit->is_selection_active()) {
  390. _replace_all();
  391. _hide_bar();
  392. }
  393. }
  394. String FindReplaceBar::get_search_text() const {
  395. return search_text->get_text();
  396. }
  397. String FindReplaceBar::get_replace_text() const {
  398. return replace_text->get_text();
  399. }
  400. bool FindReplaceBar::is_case_sensitive() const {
  401. return case_sensitive->is_pressed();
  402. }
  403. bool FindReplaceBar::is_whole_words() const {
  404. return whole_words->is_pressed();
  405. }
  406. bool FindReplaceBar::is_selection_only() const {
  407. return selection_only->is_pressed();
  408. }
  409. void FindReplaceBar::set_error(const String &p_label) {
  410. emit_signal("error", p_label);
  411. }
  412. void FindReplaceBar::set_text_edit(TextEdit *p_text_edit) {
  413. results_count = -1;
  414. text_edit = p_text_edit;
  415. text_edit->connect("text_changed", this, "_editor_text_changed");
  416. }
  417. void FindReplaceBar::_bind_methods() {
  418. ClassDB::bind_method("_unhandled_input", &FindReplaceBar::_unhandled_input);
  419. ClassDB::bind_method("_editor_text_changed", &FindReplaceBar::_editor_text_changed);
  420. ClassDB::bind_method("_search_text_changed", &FindReplaceBar::_search_text_changed);
  421. ClassDB::bind_method("_search_text_entered", &FindReplaceBar::_search_text_entered);
  422. ClassDB::bind_method("_replace_text_entered", &FindReplaceBar::_replace_text_entered);
  423. ClassDB::bind_method("_search_current", &FindReplaceBar::search_current);
  424. ClassDB::bind_method("_search_next", &FindReplaceBar::search_next);
  425. ClassDB::bind_method("_search_prev", &FindReplaceBar::search_prev);
  426. ClassDB::bind_method("_replace_pressed", &FindReplaceBar::_replace);
  427. ClassDB::bind_method("_replace_all_pressed", &FindReplaceBar::_replace_all);
  428. ClassDB::bind_method("_search_options_changed", &FindReplaceBar::_search_options_changed);
  429. ClassDB::bind_method("_hide_pressed", &FindReplaceBar::_hide_bar);
  430. ADD_SIGNAL(MethodInfo("search"));
  431. ADD_SIGNAL(MethodInfo("error"));
  432. }
  433. FindReplaceBar::FindReplaceBar() {
  434. results_count = -1;
  435. replace_all_mode = false;
  436. preserve_cursor = false;
  437. vbc_lineedit = memnew(VBoxContainer);
  438. add_child(vbc_lineedit);
  439. vbc_lineedit->set_alignment(ALIGN_CENTER);
  440. vbc_lineedit->set_h_size_flags(SIZE_EXPAND_FILL);
  441. VBoxContainer *vbc_button = memnew(VBoxContainer);
  442. add_child(vbc_button);
  443. VBoxContainer *vbc_option = memnew(VBoxContainer);
  444. add_child(vbc_option);
  445. HBoxContainer *hbc_button_search = memnew(HBoxContainer);
  446. vbc_button->add_child(hbc_button_search);
  447. hbc_button_search->set_alignment(ALIGN_END);
  448. hbc_button_replace = memnew(HBoxContainer);
  449. vbc_button->add_child(hbc_button_replace);
  450. hbc_button_replace->set_alignment(ALIGN_END);
  451. HBoxContainer *hbc_option_search = memnew(HBoxContainer);
  452. vbc_option->add_child(hbc_option_search);
  453. hbc_option_replace = memnew(HBoxContainer);
  454. vbc_option->add_child(hbc_option_replace);
  455. // search toolbar
  456. search_text = memnew(LineEdit);
  457. vbc_lineedit->add_child(search_text);
  458. search_text->set_custom_minimum_size(Size2(100 * EDSCALE, 0));
  459. search_text->connect("text_changed", this, "_search_text_changed");
  460. search_text->connect("text_entered", this, "_search_text_entered");
  461. matches_label = memnew(Label);
  462. hbc_button_search->add_child(matches_label);
  463. matches_label->hide();
  464. find_prev = memnew(ToolButton);
  465. hbc_button_search->add_child(find_prev);
  466. find_prev->set_focus_mode(FOCUS_NONE);
  467. find_prev->connect("pressed", this, "_search_prev");
  468. find_next = memnew(ToolButton);
  469. hbc_button_search->add_child(find_next);
  470. find_next->set_focus_mode(FOCUS_NONE);
  471. find_next->connect("pressed", this, "_search_next");
  472. case_sensitive = memnew(CheckBox);
  473. hbc_option_search->add_child(case_sensitive);
  474. case_sensitive->set_text(TTR("Match Case"));
  475. case_sensitive->set_focus_mode(FOCUS_NONE);
  476. case_sensitive->connect("toggled", this, "_search_options_changed");
  477. whole_words = memnew(CheckBox);
  478. hbc_option_search->add_child(whole_words);
  479. whole_words->set_text(TTR("Whole Words"));
  480. whole_words->set_focus_mode(FOCUS_NONE);
  481. whole_words->connect("toggled", this, "_search_options_changed");
  482. // replace toolbar
  483. replace_text = memnew(LineEdit);
  484. vbc_lineedit->add_child(replace_text);
  485. replace_text->set_custom_minimum_size(Size2(100 * EDSCALE, 0));
  486. replace_text->connect("text_entered", this, "_replace_text_entered");
  487. replace = memnew(Button);
  488. hbc_button_replace->add_child(replace);
  489. replace->set_text(TTR("Replace"));
  490. replace->connect("pressed", this, "_replace_pressed");
  491. replace_all = memnew(Button);
  492. hbc_button_replace->add_child(replace_all);
  493. replace_all->set_text(TTR("Replace All"));
  494. replace_all->connect("pressed", this, "_replace_all_pressed");
  495. selection_only = memnew(CheckBox);
  496. hbc_option_replace->add_child(selection_only);
  497. selection_only->set_text(TTR("Selection Only"));
  498. selection_only->set_focus_mode(FOCUS_NONE);
  499. selection_only->connect("toggled", this, "_search_options_changed");
  500. hide_button = memnew(TextureButton);
  501. add_child(hide_button);
  502. hide_button->set_focus_mode(FOCUS_NONE);
  503. hide_button->connect("pressed", this, "_hide_pressed");
  504. hide_button->set_v_size_flags(SIZE_SHRINK_CENTER);
  505. }
  506. /*** CODE EDITOR ****/
  507. // This function should be used to handle shortcuts that could otherwise
  508. // be handled too late if they weren't handled here.
  509. void CodeTextEditor::_input(const Ref<InputEvent> &event) {
  510. const Ref<InputEventKey> key_event = event;
  511. if (!key_event.is_valid() || !key_event->is_pressed())
  512. return;
  513. if (ED_IS_SHORTCUT("script_text_editor/move_up", key_event)) {
  514. move_lines_up();
  515. accept_event();
  516. return;
  517. }
  518. if (ED_IS_SHORTCUT("script_text_editor/move_down", key_event)) {
  519. move_lines_down();
  520. accept_event();
  521. return;
  522. }
  523. }
  524. void CodeTextEditor::_text_editor_gui_input(const Ref<InputEvent> &p_event) {
  525. Ref<InputEventMouseButton> mb = p_event;
  526. if (mb.is_valid()) {
  527. if (mb->is_pressed() && mb->get_command()) {
  528. if (mb->get_button_index() == BUTTON_WHEEL_UP) {
  529. _zoom_in();
  530. } else if (mb->get_button_index() == BUTTON_WHEEL_DOWN) {
  531. _zoom_out();
  532. }
  533. }
  534. }
  535. Ref<InputEventMagnifyGesture> magnify_gesture = p_event;
  536. if (magnify_gesture.is_valid()) {
  537. Ref<DynamicFont> font = text_editor->get_font("font");
  538. if (font.is_valid()) {
  539. if (font->get_size() != (int)font_size) {
  540. font_size = font->get_size();
  541. }
  542. font_size *= powf(magnify_gesture->get_factor(), 0.25);
  543. _add_font_size((int)font_size - font->get_size());
  544. }
  545. return;
  546. }
  547. Ref<InputEventKey> k = p_event;
  548. if (k.is_valid()) {
  549. if (k->is_pressed()) {
  550. if (ED_IS_SHORTCUT("script_editor/zoom_in", p_event)) {
  551. _zoom_in();
  552. }
  553. if (ED_IS_SHORTCUT("script_editor/zoom_out", p_event)) {
  554. _zoom_out();
  555. }
  556. if (ED_IS_SHORTCUT("script_editor/reset_zoom", p_event)) {
  557. _reset_zoom();
  558. }
  559. }
  560. }
  561. }
  562. void CodeTextEditor::_zoom_in() {
  563. font_resize_val += MAX(EDSCALE, 1.0f);
  564. _zoom_changed();
  565. }
  566. void CodeTextEditor::_zoom_out() {
  567. font_resize_val -= MAX(EDSCALE, 1.0f);
  568. _zoom_changed();
  569. }
  570. void CodeTextEditor::_zoom_changed() {
  571. if (font_resize_timer->get_time_left() == 0)
  572. font_resize_timer->start();
  573. }
  574. void CodeTextEditor::_reset_zoom() {
  575. Ref<DynamicFont> font = text_editor->get_font("font"); // Reset source font size to default.
  576. if (font.is_valid()) {
  577. EditorSettings::get_singleton()->set("interface/editor/code_font_size", 14);
  578. font->set_size(14);
  579. }
  580. }
  581. void CodeTextEditor::_line_col_changed() {
  582. String line = text_editor->get_line(text_editor->cursor_get_line());
  583. int positional_column = 0;
  584. for (int i = 0; i < text_editor->cursor_get_column(); i++) {
  585. if (line[i] == '\t') {
  586. positional_column += text_editor->get_indent_size(); //tab size
  587. } else {
  588. positional_column += 1;
  589. }
  590. }
  591. StringBuilder sb;
  592. sb.append("(");
  593. sb.append(itos(text_editor->cursor_get_line() + 1).lpad(3));
  594. sb.append(",");
  595. sb.append(itos(positional_column + 1).lpad(3));
  596. sb.append(")");
  597. line_and_col_txt->set_text(sb.as_string());
  598. }
  599. void CodeTextEditor::_text_changed() {
  600. if (text_editor->is_insert_text_operation()) {
  601. code_complete_timer->start();
  602. }
  603. idle->start();
  604. }
  605. void CodeTextEditor::_code_complete_timer_timeout() {
  606. if (!is_visible_in_tree())
  607. return;
  608. text_editor->query_code_comple();
  609. }
  610. void CodeTextEditor::_complete_request() {
  611. List<ScriptCodeCompletionOption> entries;
  612. String ctext = text_editor->get_text_for_completion();
  613. _code_complete_script(ctext, &entries);
  614. bool forced = false;
  615. if (code_complete_func) {
  616. code_complete_func(code_complete_ud, ctext, &entries, forced);
  617. }
  618. if (entries.size() == 0)
  619. return;
  620. for (List<ScriptCodeCompletionOption>::Element *E = entries.front(); E; E = E->next()) {
  621. E->get().icon = _get_completion_icon(E->get());
  622. }
  623. text_editor->code_complete(entries, forced);
  624. }
  625. Ref<Texture> CodeTextEditor::_get_completion_icon(const ScriptCodeCompletionOption &p_option) {
  626. Ref<Texture> tex;
  627. switch (p_option.kind) {
  628. case ScriptCodeCompletionOption::KIND_CLASS: {
  629. if (has_icon(p_option.display, "EditorIcons")) {
  630. tex = get_icon(p_option.display, "EditorIcons");
  631. } else {
  632. tex = get_icon("Object", "EditorIcons");
  633. }
  634. } break;
  635. case ScriptCodeCompletionOption::KIND_ENUM:
  636. tex = get_icon("Enum", "EditorIcons");
  637. break;
  638. case ScriptCodeCompletionOption::KIND_FILE_PATH:
  639. tex = get_icon("File", "EditorIcons");
  640. break;
  641. case ScriptCodeCompletionOption::KIND_NODE_PATH:
  642. tex = get_icon("NodePath", "EditorIcons");
  643. break;
  644. case ScriptCodeCompletionOption::KIND_VARIABLE:
  645. tex = get_icon("Variant", "EditorIcons");
  646. break;
  647. case ScriptCodeCompletionOption::KIND_CONSTANT:
  648. tex = get_icon("MemberConstant", "EditorIcons");
  649. break;
  650. case ScriptCodeCompletionOption::KIND_MEMBER:
  651. tex = get_icon("MemberProperty", "EditorIcons");
  652. break;
  653. case ScriptCodeCompletionOption::KIND_SIGNAL:
  654. tex = get_icon("MemberSignal", "EditorIcons");
  655. break;
  656. case ScriptCodeCompletionOption::KIND_FUNCTION:
  657. tex = get_icon("MemberMethod", "EditorIcons");
  658. break;
  659. case ScriptCodeCompletionOption::KIND_PLAIN_TEXT:
  660. tex = get_icon("CubeMesh", "EditorIcons");
  661. break;
  662. default:
  663. tex = get_icon("String", "EditorIcons");
  664. break;
  665. }
  666. return tex;
  667. }
  668. void CodeTextEditor::_font_resize_timeout() {
  669. if (_add_font_size(font_resize_val)) {
  670. font_resize_val = 0;
  671. }
  672. }
  673. bool CodeTextEditor::_add_font_size(int p_delta) {
  674. Ref<DynamicFont> font = text_editor->get_font("font");
  675. if (font.is_valid()) {
  676. int new_size = CLAMP(font->get_size() + p_delta, 8 * EDSCALE, 96 * EDSCALE);
  677. if (new_size != font->get_size()) {
  678. EditorSettings::get_singleton()->set("interface/editor/code_font_size", new_size / EDSCALE);
  679. font->set_size(new_size);
  680. }
  681. return true;
  682. } else {
  683. return false;
  684. }
  685. }
  686. void CodeTextEditor::update_editor_settings() {
  687. text_editor->set_syntax_coloring(EditorSettings::get_singleton()->get("text_editor/highlighting/syntax_highlighting"));
  688. text_editor->set_highlight_all_occurrences(EditorSettings::get_singleton()->get("text_editor/highlighting/highlight_all_occurrences"));
  689. text_editor->set_highlight_current_line(EditorSettings::get_singleton()->get("text_editor/highlighting/highlight_current_line"));
  690. text_editor->set_indent_using_spaces(EditorSettings::get_singleton()->get("text_editor/indent/type"));
  691. text_editor->set_indent_size(EditorSettings::get_singleton()->get("text_editor/indent/size"));
  692. text_editor->set_auto_indent(EditorSettings::get_singleton()->get("text_editor/indent/auto_indent"));
  693. text_editor->set_draw_tabs(EditorSettings::get_singleton()->get("text_editor/indent/draw_tabs"));
  694. text_editor->set_draw_spaces(EditorSettings::get_singleton()->get("text_editor/indent/draw_spaces"));
  695. text_editor->set_smooth_scroll_enabled(EditorSettings::get_singleton()->get("text_editor/navigation/smooth_scrolling"));
  696. text_editor->set_v_scroll_speed(EditorSettings::get_singleton()->get("text_editor/navigation/v_scroll_speed"));
  697. text_editor->set_draw_minimap(EditorSettings::get_singleton()->get("text_editor/navigation/show_minimap"));
  698. text_editor->set_minimap_width(EditorSettings::get_singleton()->get("text_editor/navigation/minimap_width"));
  699. text_editor->set_show_line_numbers(EditorSettings::get_singleton()->get("text_editor/appearance/show_line_numbers"));
  700. text_editor->set_line_numbers_zero_padded(EditorSettings::get_singleton()->get("text_editor/appearance/line_numbers_zero_padded"));
  701. text_editor->set_bookmark_gutter_enabled(EditorSettings::get_singleton()->get("text_editor/appearance/show_bookmark_gutter"));
  702. text_editor->set_breakpoint_gutter_enabled(EditorSettings::get_singleton()->get("text_editor/appearance/show_breakpoint_gutter"));
  703. text_editor->set_draw_info_gutter(EditorSettings::get_singleton()->get("text_editor/appearance/show_info_gutter"));
  704. text_editor->set_hiding_enabled(EditorSettings::get_singleton()->get("text_editor/appearance/code_folding"));
  705. text_editor->set_draw_fold_gutter(EditorSettings::get_singleton()->get("text_editor/appearance/code_folding"));
  706. text_editor->set_wrap_enabled(EditorSettings::get_singleton()->get("text_editor/appearance/word_wrap"));
  707. text_editor->set_show_line_length_guideline(EditorSettings::get_singleton()->get("text_editor/appearance/show_line_length_guideline"));
  708. text_editor->set_line_length_guideline_column(EditorSettings::get_singleton()->get("text_editor/appearance/line_length_guideline_column"));
  709. text_editor->set_scroll_pass_end_of_file(EditorSettings::get_singleton()->get("text_editor/cursor/scroll_past_end_of_file"));
  710. text_editor->cursor_set_block_mode(EditorSettings::get_singleton()->get("text_editor/cursor/block_caret"));
  711. text_editor->cursor_set_blink_enabled(EditorSettings::get_singleton()->get("text_editor/cursor/caret_blink"));
  712. text_editor->cursor_set_blink_speed(EditorSettings::get_singleton()->get("text_editor/cursor/caret_blink_speed"));
  713. text_editor->set_auto_brace_completion(EditorSettings::get_singleton()->get("text_editor/completion/auto_brace_complete"));
  714. }
  715. void CodeTextEditor::trim_trailing_whitespace() {
  716. bool trimed_whitespace = false;
  717. for (int i = 0; i < text_editor->get_line_count(); i++) {
  718. String line = text_editor->get_line(i);
  719. if (line.ends_with(" ") || line.ends_with("\t")) {
  720. if (!trimed_whitespace) {
  721. text_editor->begin_complex_operation();
  722. trimed_whitespace = true;
  723. }
  724. int end = 0;
  725. for (int j = line.length() - 1; j > -1; j--) {
  726. if (line[j] != ' ' && line[j] != '\t') {
  727. end = j + 1;
  728. break;
  729. }
  730. }
  731. text_editor->set_line(i, line.substr(0, end));
  732. }
  733. }
  734. if (trimed_whitespace) {
  735. text_editor->end_complex_operation();
  736. text_editor->update();
  737. }
  738. }
  739. void CodeTextEditor::insert_final_newline() {
  740. int final_line = text_editor->get_line_count() - 1;
  741. String line = text_editor->get_line(final_line);
  742. //length 0 means it's already an empty line,
  743. //no need to add a newline
  744. if (line.length() > 0 && !line.ends_with("\n")) {
  745. text_editor->begin_complex_operation();
  746. line += "\n";
  747. text_editor->set_line(final_line, line);
  748. text_editor->end_complex_operation();
  749. text_editor->update();
  750. }
  751. }
  752. void CodeTextEditor::convert_indent_to_spaces() {
  753. int indent_size = EditorSettings::get_singleton()->get("text_editor/indent/size");
  754. String indent = "";
  755. for (int i = 0; i < indent_size; i++) {
  756. indent += " ";
  757. }
  758. int cursor_line = text_editor->cursor_get_line();
  759. int cursor_column = text_editor->cursor_get_column();
  760. bool changed_indentation = false;
  761. for (int i = 0; i < text_editor->get_line_count(); i++) {
  762. String line = text_editor->get_line(i);
  763. if (line.length() <= 0) {
  764. continue;
  765. }
  766. int j = 0;
  767. while (j < line.length() && (line[j] == ' ' || line[j] == '\t')) {
  768. if (line[j] == '\t') {
  769. if (!changed_indentation) {
  770. text_editor->begin_complex_operation();
  771. changed_indentation = true;
  772. }
  773. if (cursor_line == i && cursor_column > j) {
  774. cursor_column += indent_size - 1;
  775. }
  776. line = line.left(j) + indent + line.right(j + 1);
  777. }
  778. j++;
  779. }
  780. if (changed_indentation) {
  781. text_editor->set_line(i, line);
  782. }
  783. }
  784. if (changed_indentation) {
  785. text_editor->cursor_set_column(cursor_column);
  786. text_editor->end_complex_operation();
  787. text_editor->update();
  788. }
  789. }
  790. void CodeTextEditor::convert_indent_to_tabs() {
  791. int indent_size = EditorSettings::get_singleton()->get("text_editor/indent/size");
  792. indent_size -= 1;
  793. int cursor_line = text_editor->cursor_get_line();
  794. int cursor_column = text_editor->cursor_get_column();
  795. bool changed_indentation = false;
  796. for (int i = 0; i < text_editor->get_line_count(); i++) {
  797. String line = text_editor->get_line(i);
  798. if (line.length() <= 0) {
  799. continue;
  800. }
  801. int j = 0;
  802. int space_count = -1;
  803. while (j < line.length() && (line[j] == ' ' || line[j] == '\t')) {
  804. if (line[j] != '\t') {
  805. space_count++;
  806. if (space_count == indent_size) {
  807. if (!changed_indentation) {
  808. text_editor->begin_complex_operation();
  809. changed_indentation = true;
  810. }
  811. if (cursor_line == i && cursor_column > j) {
  812. cursor_column -= indent_size;
  813. }
  814. line = line.left(j - indent_size) + "\t" + line.right(j + 1);
  815. j = 0;
  816. space_count = -1;
  817. }
  818. } else {
  819. space_count = -1;
  820. }
  821. j++;
  822. }
  823. if (changed_indentation) {
  824. text_editor->set_line(i, line);
  825. }
  826. }
  827. if (changed_indentation) {
  828. text_editor->cursor_set_column(cursor_column);
  829. text_editor->end_complex_operation();
  830. text_editor->update();
  831. }
  832. }
  833. void CodeTextEditor::convert_case(CaseStyle p_case) {
  834. if (!text_editor->is_selection_active()) {
  835. return;
  836. }
  837. text_editor->begin_complex_operation();
  838. int begin = text_editor->get_selection_from_line();
  839. int end = text_editor->get_selection_to_line();
  840. int begin_col = text_editor->get_selection_from_column();
  841. int end_col = text_editor->get_selection_to_column();
  842. for (int i = begin; i <= end; i++) {
  843. int len = text_editor->get_line(i).length();
  844. if (i == end)
  845. len = end_col;
  846. if (i == begin)
  847. len -= begin_col;
  848. String new_line = text_editor->get_line(i).substr(i == begin ? begin_col : 0, len);
  849. switch (p_case) {
  850. case UPPER: {
  851. new_line = new_line.to_upper();
  852. } break;
  853. case LOWER: {
  854. new_line = new_line.to_lower();
  855. } break;
  856. case CAPITALIZE: {
  857. new_line = new_line.capitalize();
  858. } break;
  859. }
  860. if (i == begin) {
  861. new_line = text_editor->get_line(i).left(begin_col) + new_line;
  862. }
  863. if (i == end) {
  864. new_line = new_line + text_editor->get_line(i).right(end_col);
  865. }
  866. text_editor->set_line(i, new_line);
  867. }
  868. text_editor->end_complex_operation();
  869. }
  870. void CodeTextEditor::move_lines_up() {
  871. text_editor->begin_complex_operation();
  872. if (text_editor->is_selection_active()) {
  873. int from_line = text_editor->get_selection_from_line();
  874. int from_col = text_editor->get_selection_from_column();
  875. int to_line = text_editor->get_selection_to_line();
  876. int to_column = text_editor->get_selection_to_column();
  877. for (int i = from_line; i <= to_line; i++) {
  878. int line_id = i;
  879. int next_id = i - 1;
  880. if (line_id == 0 || next_id < 0)
  881. return;
  882. text_editor->unfold_line(line_id);
  883. text_editor->unfold_line(next_id);
  884. text_editor->swap_lines(line_id, next_id);
  885. text_editor->cursor_set_line(next_id);
  886. }
  887. int from_line_up = from_line > 0 ? from_line - 1 : from_line;
  888. int to_line_up = to_line > 0 ? to_line - 1 : to_line;
  889. text_editor->select(from_line_up, from_col, to_line_up, to_column);
  890. } else {
  891. int line_id = text_editor->cursor_get_line();
  892. int next_id = line_id - 1;
  893. if (line_id == 0 || next_id < 0)
  894. return;
  895. text_editor->unfold_line(line_id);
  896. text_editor->unfold_line(next_id);
  897. text_editor->swap_lines(line_id, next_id);
  898. text_editor->cursor_set_line(next_id);
  899. }
  900. text_editor->end_complex_operation();
  901. text_editor->update();
  902. }
  903. void CodeTextEditor::move_lines_down() {
  904. text_editor->begin_complex_operation();
  905. if (text_editor->is_selection_active()) {
  906. int from_line = text_editor->get_selection_from_line();
  907. int from_col = text_editor->get_selection_from_column();
  908. int to_line = text_editor->get_selection_to_line();
  909. int to_column = text_editor->get_selection_to_column();
  910. for (int i = to_line; i >= from_line; i--) {
  911. int line_id = i;
  912. int next_id = i + 1;
  913. if (line_id == text_editor->get_line_count() - 1 || next_id > text_editor->get_line_count())
  914. return;
  915. text_editor->unfold_line(line_id);
  916. text_editor->unfold_line(next_id);
  917. text_editor->swap_lines(line_id, next_id);
  918. text_editor->cursor_set_line(next_id);
  919. }
  920. int from_line_down = from_line < text_editor->get_line_count() ? from_line + 1 : from_line;
  921. int to_line_down = to_line < text_editor->get_line_count() ? to_line + 1 : to_line;
  922. text_editor->select(from_line_down, from_col, to_line_down, to_column);
  923. } else {
  924. int line_id = text_editor->cursor_get_line();
  925. int next_id = line_id + 1;
  926. if (line_id == text_editor->get_line_count() - 1 || next_id > text_editor->get_line_count())
  927. return;
  928. text_editor->unfold_line(line_id);
  929. text_editor->unfold_line(next_id);
  930. text_editor->swap_lines(line_id, next_id);
  931. text_editor->cursor_set_line(next_id);
  932. }
  933. text_editor->end_complex_operation();
  934. text_editor->update();
  935. }
  936. void CodeTextEditor::delete_lines() {
  937. text_editor->begin_complex_operation();
  938. if (text_editor->is_selection_active()) {
  939. int to_line = text_editor->get_selection_to_line();
  940. int from_line = text_editor->get_selection_from_line();
  941. int count = Math::abs(to_line - from_line) + 1;
  942. text_editor->cursor_set_line(to_line, false);
  943. while (count) {
  944. text_editor->set_line(text_editor->cursor_get_line(), "");
  945. text_editor->backspace_at_cursor();
  946. count--;
  947. if (count)
  948. text_editor->unfold_line(from_line);
  949. }
  950. text_editor->cursor_set_line(from_line - 1);
  951. text_editor->deselect();
  952. } else {
  953. int line = text_editor->cursor_get_line();
  954. text_editor->set_line(text_editor->cursor_get_line(), "");
  955. text_editor->backspace_at_cursor();
  956. text_editor->unfold_line(line);
  957. text_editor->cursor_set_line(line);
  958. }
  959. text_editor->end_complex_operation();
  960. }
  961. void CodeTextEditor::clone_lines_down() {
  962. const int cursor_column = text_editor->cursor_get_column();
  963. int from_line = text_editor->cursor_get_line();
  964. int to_line = text_editor->cursor_get_line();
  965. int from_column = 0;
  966. int to_column = 0;
  967. int cursor_new_line = to_line + 1;
  968. int cursor_new_column = text_editor->cursor_get_column();
  969. String new_text = "\n" + text_editor->get_line(from_line);
  970. bool selection_active = false;
  971. text_editor->cursor_set_column(text_editor->get_line(from_line).length());
  972. if (text_editor->is_selection_active()) {
  973. from_column = text_editor->get_selection_from_column();
  974. to_column = text_editor->get_selection_to_column();
  975. from_line = text_editor->get_selection_from_line();
  976. to_line = text_editor->get_selection_to_line();
  977. cursor_new_line = to_line + text_editor->cursor_get_line() - from_line;
  978. cursor_new_column = to_column == cursor_column ? 2 * to_column - from_column : to_column;
  979. new_text = text_editor->get_selection_text();
  980. selection_active = true;
  981. text_editor->cursor_set_line(to_line);
  982. text_editor->cursor_set_column(to_column);
  983. }
  984. text_editor->begin_complex_operation();
  985. for (int i = from_line; i <= to_line; i++) {
  986. text_editor->unfold_line(i);
  987. }
  988. text_editor->deselect();
  989. text_editor->insert_text_at_cursor(new_text);
  990. text_editor->cursor_set_line(cursor_new_line);
  991. text_editor->cursor_set_column(cursor_new_column);
  992. if (selection_active) {
  993. text_editor->select(to_line, to_column, 2 * to_line - from_line, 2 * to_column - from_column);
  994. }
  995. text_editor->end_complex_operation();
  996. text_editor->update();
  997. }
  998. void CodeTextEditor::toggle_inline_comment(const String &delimiter) {
  999. text_editor->begin_complex_operation();
  1000. if (text_editor->is_selection_active()) {
  1001. int begin = text_editor->get_selection_from_line();
  1002. int end = text_editor->get_selection_to_line();
  1003. // End of selection ends on the first column of the last line, ignore it.
  1004. if (text_editor->get_selection_to_column() == 0)
  1005. end -= 1;
  1006. int col_to = text_editor->get_selection_to_column();
  1007. int cursor_pos = text_editor->cursor_get_column();
  1008. // Check if all lines in the selected block are commented.
  1009. bool is_commented = true;
  1010. for (int i = begin; i <= end; i++) {
  1011. if (!text_editor->get_line(i).begins_with(delimiter)) {
  1012. is_commented = false;
  1013. break;
  1014. }
  1015. }
  1016. for (int i = begin; i <= end; i++) {
  1017. String line_text = text_editor->get_line(i);
  1018. if (line_text.strip_edges().empty()) {
  1019. line_text = delimiter;
  1020. } else {
  1021. if (is_commented) {
  1022. line_text = line_text.substr(delimiter.length(), line_text.length());
  1023. } else {
  1024. line_text = delimiter + line_text;
  1025. }
  1026. }
  1027. text_editor->set_line(i, line_text);
  1028. }
  1029. // Adjust selection & cursor position.
  1030. int offset = (is_commented ? -1 : 1) * delimiter.length();
  1031. int col_from = text_editor->get_selection_from_column() > 0 ? text_editor->get_selection_from_column() + offset : 0;
  1032. if (is_commented && text_editor->cursor_get_column() == text_editor->get_line(text_editor->cursor_get_line()).length() + 1)
  1033. cursor_pos += 1;
  1034. if (text_editor->get_selection_to_column() != 0 && col_to != text_editor->get_line(text_editor->get_selection_to_line()).length() + 1)
  1035. col_to += offset;
  1036. if (text_editor->cursor_get_column() != 0)
  1037. cursor_pos += offset;
  1038. text_editor->select(begin, col_from, text_editor->get_selection_to_line(), col_to);
  1039. text_editor->cursor_set_column(cursor_pos);
  1040. } else {
  1041. int begin = text_editor->cursor_get_line();
  1042. String line_text = text_editor->get_line(begin);
  1043. int delimiter_length = delimiter.length();
  1044. int col = text_editor->cursor_get_column();
  1045. if (line_text.begins_with(delimiter)) {
  1046. line_text = line_text.substr(delimiter_length, line_text.length());
  1047. col -= delimiter_length;
  1048. } else {
  1049. line_text = delimiter + line_text;
  1050. col += delimiter_length;
  1051. }
  1052. text_editor->set_line(begin, line_text);
  1053. text_editor->cursor_set_column(col);
  1054. }
  1055. text_editor->end_complex_operation();
  1056. text_editor->update();
  1057. }
  1058. void CodeTextEditor::goto_line(int p_line) {
  1059. text_editor->deselect();
  1060. text_editor->unfold_line(p_line);
  1061. text_editor->call_deferred("cursor_set_line", p_line);
  1062. }
  1063. void CodeTextEditor::goto_line_selection(int p_line, int p_begin, int p_end) {
  1064. text_editor->unfold_line(p_line);
  1065. text_editor->call_deferred("cursor_set_line", p_line);
  1066. text_editor->call_deferred("cursor_set_column", p_begin);
  1067. text_editor->select(p_line, p_begin, p_line, p_end);
  1068. }
  1069. void CodeTextEditor::goto_line_centered(int p_line) {
  1070. goto_line(p_line);
  1071. text_editor->call_deferred("center_viewport_to_cursor");
  1072. }
  1073. void CodeTextEditor::set_executing_line(int p_line) {
  1074. text_editor->set_executing_line(p_line);
  1075. }
  1076. void CodeTextEditor::clear_executing_line() {
  1077. text_editor->clear_executing_line();
  1078. }
  1079. Variant CodeTextEditor::get_edit_state() {
  1080. Dictionary state;
  1081. state["scroll_position"] = text_editor->get_v_scroll();
  1082. state["h_scroll_position"] = text_editor->get_h_scroll();
  1083. state["column"] = text_editor->cursor_get_column();
  1084. state["row"] = text_editor->cursor_get_line();
  1085. state["selection"] = get_text_edit()->is_selection_active();
  1086. if (get_text_edit()->is_selection_active()) {
  1087. state["selection_from_line"] = text_editor->get_selection_from_line();
  1088. state["selection_from_column"] = text_editor->get_selection_from_column();
  1089. state["selection_to_line"] = text_editor->get_selection_to_line();
  1090. state["selection_to_column"] = text_editor->get_selection_to_column();
  1091. }
  1092. state["folded_lines"] = text_editor->get_folded_lines();
  1093. state["breakpoints"] = text_editor->get_breakpoints_array();
  1094. state["bookmarks"] = text_editor->get_bookmarks_array();
  1095. state["syntax_highlighter"] = TTR("Standard");
  1096. SyntaxHighlighter *syntax_highlighter = text_editor->_get_syntax_highlighting();
  1097. if (syntax_highlighter) {
  1098. state["syntax_highlighter"] = syntax_highlighter->get_name();
  1099. }
  1100. return state;
  1101. }
  1102. void CodeTextEditor::set_edit_state(const Variant &p_state) {
  1103. Dictionary state = p_state;
  1104. /* update the row first as it sets the column to 0 */
  1105. text_editor->cursor_set_line(state["row"]);
  1106. text_editor->cursor_set_column(state["column"]);
  1107. text_editor->set_v_scroll(state["scroll_position"]);
  1108. text_editor->set_h_scroll(state["h_scroll_position"]);
  1109. if (state.has("selection")) {
  1110. text_editor->select(state["selection_from_line"], state["selection_from_column"], state["selection_to_line"], state["selection_to_column"]);
  1111. }
  1112. if (state.has("folded_lines")) {
  1113. Vector<int> folded_lines = state["folded_lines"];
  1114. for (int i = 0; i < folded_lines.size(); i++) {
  1115. text_editor->fold_line(folded_lines[i]);
  1116. }
  1117. }
  1118. if (state.has("breakpoints")) {
  1119. Array breakpoints = state["breakpoints"];
  1120. for (int i = 0; i < breakpoints.size(); i++) {
  1121. text_editor->set_line_as_breakpoint(breakpoints[i], true);
  1122. }
  1123. }
  1124. if (state.has("bookmarks")) {
  1125. Array bookmarks = state["bookmarks"];
  1126. for (int i = 0; i < bookmarks.size(); i++) {
  1127. text_editor->set_line_as_bookmark(bookmarks[i], true);
  1128. }
  1129. }
  1130. text_editor->grab_focus();
  1131. }
  1132. void CodeTextEditor::set_error(const String &p_error) {
  1133. error->set_text(p_error);
  1134. if (p_error != "") {
  1135. error->set_default_cursor_shape(CURSOR_POINTING_HAND);
  1136. } else {
  1137. error->set_default_cursor_shape(CURSOR_ARROW);
  1138. }
  1139. }
  1140. void CodeTextEditor::set_error_pos(int p_line, int p_column) {
  1141. error_line = p_line;
  1142. error_column = p_column;
  1143. }
  1144. void CodeTextEditor::goto_error() {
  1145. if (error->get_text() != "") {
  1146. text_editor->cursor_set_line(error_line);
  1147. text_editor->cursor_set_column(error_column);
  1148. text_editor->center_viewport_to_cursor();
  1149. }
  1150. }
  1151. void CodeTextEditor::_update_font() {
  1152. text_editor->add_font_override("font", get_font("source", "EditorFonts"));
  1153. error->add_font_override("font", get_font("status_source", "EditorFonts"));
  1154. error->add_color_override("font_color", get_color("error_color", "Editor"));
  1155. Ref<Font> status_bar_font = get_font("status_source", "EditorFonts");
  1156. error->add_font_override("font", status_bar_font);
  1157. int count = status_bar->get_child_count();
  1158. for (int i = 0; i < count; i++) {
  1159. Control *n = Object::cast_to<Control>(status_bar->get_child(i));
  1160. if (n)
  1161. n->add_font_override("font", status_bar_font);
  1162. }
  1163. }
  1164. void CodeTextEditor::_on_settings_change() {
  1165. _update_font();
  1166. font_size = EditorSettings::get_singleton()->get("interface/editor/code_font_size");
  1167. // Auto brace completion.
  1168. text_editor->set_auto_brace_completion(
  1169. EDITOR_GET("text_editor/completion/auto_brace_complete"));
  1170. code_complete_timer->set_wait_time(
  1171. EDITOR_GET("text_editor/completion/code_complete_delay"));
  1172. // Call hint settings.
  1173. text_editor->set_callhint_settings(
  1174. EDITOR_GET("text_editor/completion/put_callhint_tooltip_below_current_line"),
  1175. EDITOR_GET("text_editor/completion/callhint_tooltip_offset"));
  1176. idle->set_wait_time(EDITOR_GET("text_editor/completion/idle_parse_delay"));
  1177. }
  1178. void CodeTextEditor::_text_changed_idle_timeout() {
  1179. _validate_script();
  1180. emit_signal("validate_script");
  1181. }
  1182. void CodeTextEditor::validate_script() {
  1183. idle->start();
  1184. }
  1185. void CodeTextEditor::_warning_label_gui_input(const Ref<InputEvent> &p_event) {
  1186. Ref<InputEventMouseButton> mb = p_event;
  1187. if (mb.is_valid() && mb->is_pressed() && mb->get_button_index() == BUTTON_LEFT) {
  1188. _warning_button_pressed();
  1189. }
  1190. }
  1191. void CodeTextEditor::_warning_button_pressed() {
  1192. _set_show_warnings_panel(!is_warnings_panel_opened);
  1193. }
  1194. void CodeTextEditor::_set_show_warnings_panel(bool p_show) {
  1195. is_warnings_panel_opened = p_show;
  1196. emit_signal("show_warnings_panel", p_show);
  1197. }
  1198. void CodeTextEditor::_error_pressed(const Ref<InputEvent> &p_event) {
  1199. Ref<InputEventMouseButton> mb = p_event;
  1200. if (mb.is_valid() && mb->is_pressed() && mb->get_button_index() == BUTTON_LEFT) {
  1201. emit_signal("error_pressed");
  1202. }
  1203. }
  1204. void CodeTextEditor::_notification(int p_what) {
  1205. switch (p_what) {
  1206. case EditorSettings::NOTIFICATION_EDITOR_SETTINGS_CHANGED: {
  1207. _load_theme_settings();
  1208. emit_signal("load_theme_settings");
  1209. } break;
  1210. case NOTIFICATION_THEME_CHANGED: {
  1211. _update_font();
  1212. } break;
  1213. case NOTIFICATION_ENTER_TREE: {
  1214. warning_button->set_icon(get_icon("NodeWarning", "EditorIcons"));
  1215. add_constant_override("separation", 4 * EDSCALE);
  1216. } break;
  1217. case NOTIFICATION_VISIBILITY_CHANGED: {
  1218. set_process_input(is_visible_in_tree());
  1219. } break;
  1220. default:
  1221. break;
  1222. }
  1223. }
  1224. void CodeTextEditor::set_warning_nb(int p_warning_nb) {
  1225. warning_count_label->set_text(itos(p_warning_nb));
  1226. warning_count_label->set_visible(p_warning_nb > 0);
  1227. warning_button->set_visible(p_warning_nb > 0);
  1228. if (!p_warning_nb)
  1229. _set_show_warnings_panel(false);
  1230. }
  1231. void CodeTextEditor::toggle_bookmark() {
  1232. int line = text_editor->cursor_get_line();
  1233. text_editor->set_line_as_bookmark(line, !text_editor->is_line_set_as_bookmark(line));
  1234. }
  1235. void CodeTextEditor::goto_next_bookmark() {
  1236. List<int> bmarks;
  1237. text_editor->get_bookmarks(&bmarks);
  1238. if (bmarks.size() <= 0) {
  1239. return;
  1240. }
  1241. int line = text_editor->cursor_get_line();
  1242. if (line >= bmarks[bmarks.size() - 1]) {
  1243. text_editor->unfold_line(bmarks[0]);
  1244. text_editor->cursor_set_line(bmarks[0]);
  1245. } else {
  1246. for (List<int>::Element *E = bmarks.front(); E; E = E->next()) {
  1247. int bline = E->get();
  1248. if (bline > line) {
  1249. text_editor->unfold_line(bline);
  1250. text_editor->cursor_set_line(bline);
  1251. return;
  1252. }
  1253. }
  1254. }
  1255. }
  1256. void CodeTextEditor::goto_prev_bookmark() {
  1257. List<int> bmarks;
  1258. text_editor->get_bookmarks(&bmarks);
  1259. if (bmarks.size() <= 0) {
  1260. return;
  1261. }
  1262. int line = text_editor->cursor_get_line();
  1263. if (line <= bmarks[0]) {
  1264. text_editor->unfold_line(bmarks[bmarks.size() - 1]);
  1265. text_editor->cursor_set_line(bmarks[bmarks.size() - 1]);
  1266. } else {
  1267. for (List<int>::Element *E = bmarks.back(); E; E = E->prev()) {
  1268. int bline = E->get();
  1269. if (bline < line) {
  1270. text_editor->unfold_line(bline);
  1271. text_editor->cursor_set_line(bline);
  1272. return;
  1273. }
  1274. }
  1275. }
  1276. }
  1277. void CodeTextEditor::remove_all_bookmarks() {
  1278. List<int> bmarks;
  1279. text_editor->get_bookmarks(&bmarks);
  1280. for (List<int>::Element *E = bmarks.front(); E; E = E->next()) {
  1281. text_editor->set_line_as_bookmark(E->get(), false);
  1282. }
  1283. }
  1284. void CodeTextEditor::_bind_methods() {
  1285. ClassDB::bind_method(D_METHOD("_input"), &CodeTextEditor::_input);
  1286. ClassDB::bind_method("_text_editor_gui_input", &CodeTextEditor::_text_editor_gui_input);
  1287. ClassDB::bind_method("_line_col_changed", &CodeTextEditor::_line_col_changed);
  1288. ClassDB::bind_method("_text_changed", &CodeTextEditor::_text_changed);
  1289. ClassDB::bind_method("_on_settings_change", &CodeTextEditor::_on_settings_change);
  1290. ClassDB::bind_method("_text_changed_idle_timeout", &CodeTextEditor::_text_changed_idle_timeout);
  1291. ClassDB::bind_method("_code_complete_timer_timeout", &CodeTextEditor::_code_complete_timer_timeout);
  1292. ClassDB::bind_method("_complete_request", &CodeTextEditor::_complete_request);
  1293. ClassDB::bind_method("_font_resize_timeout", &CodeTextEditor::_font_resize_timeout);
  1294. ClassDB::bind_method("_error_pressed", &CodeTextEditor::_error_pressed);
  1295. ClassDB::bind_method("_warning_button_pressed", &CodeTextEditor::_warning_button_pressed);
  1296. ClassDB::bind_method("_warning_label_gui_input", &CodeTextEditor::_warning_label_gui_input);
  1297. ADD_SIGNAL(MethodInfo("validate_script"));
  1298. ADD_SIGNAL(MethodInfo("load_theme_settings"));
  1299. ADD_SIGNAL(MethodInfo("show_warnings_panel"));
  1300. ADD_SIGNAL(MethodInfo("error_pressed"));
  1301. }
  1302. void CodeTextEditor::set_code_complete_func(CodeTextEditorCodeCompleteFunc p_code_complete_func, void *p_ud) {
  1303. code_complete_func = p_code_complete_func;
  1304. code_complete_ud = p_ud;
  1305. }
  1306. CodeTextEditor::CodeTextEditor() {
  1307. code_complete_func = NULL;
  1308. ED_SHORTCUT("script_editor/zoom_in", TTR("Zoom In"), KEY_MASK_CMD | KEY_EQUAL);
  1309. ED_SHORTCUT("script_editor/zoom_out", TTR("Zoom Out"), KEY_MASK_CMD | KEY_MINUS);
  1310. ED_SHORTCUT("script_editor/reset_zoom", TTR("Reset Zoom"), KEY_MASK_CMD | KEY_0);
  1311. text_editor = memnew(TextEdit);
  1312. add_child(text_editor);
  1313. text_editor->set_v_size_flags(SIZE_EXPAND_FILL);
  1314. // Added second so it opens at the bottom, so it won't shift the entire text editor when opening.
  1315. find_replace_bar = memnew(FindReplaceBar);
  1316. add_child(find_replace_bar);
  1317. find_replace_bar->set_h_size_flags(SIZE_EXPAND_FILL);
  1318. find_replace_bar->hide();
  1319. find_replace_bar->set_text_edit(text_editor);
  1320. text_editor->set_show_line_numbers(true);
  1321. text_editor->set_brace_matching(true);
  1322. text_editor->set_auto_indent(true);
  1323. status_bar = memnew(HBoxContainer);
  1324. add_child(status_bar);
  1325. status_bar->set_h_size_flags(SIZE_EXPAND_FILL);
  1326. status_bar->set_custom_minimum_size(Size2(0, 24 * EDSCALE)); // Adjust for the height of the warning icon.
  1327. idle = memnew(Timer);
  1328. add_child(idle);
  1329. idle->set_one_shot(true);
  1330. idle->set_wait_time(EDITOR_GET("text_editor/completion/idle_parse_delay"));
  1331. code_complete_timer = memnew(Timer);
  1332. add_child(code_complete_timer);
  1333. code_complete_timer->set_one_shot(true);
  1334. code_complete_timer->set_wait_time(EDITOR_GET("text_editor/completion/code_complete_delay"));
  1335. error_line = 0;
  1336. error_column = 0;
  1337. // Error
  1338. ScrollContainer *scroll = memnew(ScrollContainer);
  1339. scroll->set_h_size_flags(SIZE_EXPAND_FILL);
  1340. scroll->set_v_size_flags(SIZE_EXPAND_FILL);
  1341. scroll->set_enable_v_scroll(false);
  1342. status_bar->add_child(scroll);
  1343. error = memnew(Label);
  1344. scroll->add_child(error);
  1345. error->set_v_size_flags(SIZE_EXPAND | SIZE_SHRINK_CENTER);
  1346. error->set_mouse_filter(MOUSE_FILTER_STOP);
  1347. error->connect("gui_input", this, "_error_pressed");
  1348. find_replace_bar->connect("error", error, "set_text");
  1349. // Warnings
  1350. warning_button = memnew(ToolButton);
  1351. status_bar->add_child(warning_button);
  1352. warning_button->set_v_size_flags(SIZE_EXPAND | SIZE_SHRINK_CENTER);
  1353. warning_button->set_default_cursor_shape(CURSOR_POINTING_HAND);
  1354. warning_button->connect("pressed", this, "_warning_button_pressed");
  1355. warning_button->set_tooltip(TTR("Warnings"));
  1356. warning_count_label = memnew(Label);
  1357. status_bar->add_child(warning_count_label);
  1358. warning_count_label->set_v_size_flags(SIZE_EXPAND | SIZE_SHRINK_CENTER);
  1359. warning_count_label->set_align(Label::ALIGN_RIGHT);
  1360. warning_count_label->set_default_cursor_shape(CURSOR_POINTING_HAND);
  1361. warning_count_label->set_mouse_filter(MOUSE_FILTER_STOP);
  1362. warning_count_label->set_tooltip(TTR("Warnings"));
  1363. warning_count_label->add_color_override("font_color", EditorNode::get_singleton()->get_gui_base()->get_color("warning_color", "Editor"));
  1364. warning_count_label->add_font_override("font", EditorNode::get_singleton()->get_gui_base()->get_font("status_source", "EditorFonts"));
  1365. warning_count_label->connect("gui_input", this, "_warning_label_gui_input");
  1366. is_warnings_panel_opened = false;
  1367. set_warning_nb(0);
  1368. // Line and column
  1369. line_and_col_txt = memnew(Label);
  1370. status_bar->add_child(line_and_col_txt);
  1371. line_and_col_txt->set_v_size_flags(SIZE_EXPAND | SIZE_SHRINK_CENTER);
  1372. line_and_col_txt->add_font_override("font", EditorNode::get_singleton()->get_gui_base()->get_font("status_source", "EditorFonts"));
  1373. line_and_col_txt->set_tooltip(TTR("Line and column numbers."));
  1374. line_and_col_txt->set_mouse_filter(MOUSE_FILTER_STOP);
  1375. text_editor->connect("gui_input", this, "_text_editor_gui_input");
  1376. text_editor->connect("cursor_changed", this, "_line_col_changed");
  1377. text_editor->connect("text_changed", this, "_text_changed");
  1378. text_editor->connect("request_completion", this, "_complete_request");
  1379. Vector<String> cs;
  1380. cs.push_back(".");
  1381. cs.push_back(",");
  1382. cs.push_back("(");
  1383. cs.push_back("=");
  1384. cs.push_back("$");
  1385. text_editor->set_completion(true, cs);
  1386. idle->connect("timeout", this, "_text_changed_idle_timeout");
  1387. code_complete_timer->connect("timeout", this, "_code_complete_timer_timeout");
  1388. font_resize_val = 0;
  1389. font_size = EditorSettings::get_singleton()->get("interface/editor/code_font_size");
  1390. font_resize_timer = memnew(Timer);
  1391. add_child(font_resize_timer);
  1392. font_resize_timer->set_one_shot(true);
  1393. font_resize_timer->set_wait_time(0.07);
  1394. font_resize_timer->connect("timeout", this, "_font_resize_timeout");
  1395. EditorSettings::get_singleton()->connect("settings_changed", this, "_on_settings_change");
  1396. }