code_editor.cpp 57 KB

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