code_editor.cpp 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247
  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 "editor/editor_scale.h"
  32. #include "editor_settings.h"
  33. #include "os/keyboard.h"
  34. #include "scene/gui/margin_container.h"
  35. #include "scene/gui/separator.h"
  36. #include "scene/resources/dynamic_font.h"
  37. void GotoLineDialog::popup_find_line(TextEdit *p_edit) {
  38. text_editor = p_edit;
  39. line->set_text(itos(text_editor->cursor_get_line()));
  40. line->select_all();
  41. popup_centered(Size2(180, 80));
  42. line->grab_focus();
  43. }
  44. int GotoLineDialog::get_line() const {
  45. return line->get_text().to_int();
  46. }
  47. void GotoLineDialog::ok_pressed() {
  48. if (get_line() < 1 || get_line() > text_editor->get_line_count())
  49. return;
  50. text_editor->cursor_set_line(get_line() - 1);
  51. hide();
  52. }
  53. GotoLineDialog::GotoLineDialog() {
  54. set_title(TTR("Go to Line"));
  55. Label *l = memnew(Label);
  56. l->set_text(TTR("Line Number:"));
  57. l->set_pos(Point2(5, 5));
  58. add_child(l);
  59. line = memnew(LineEdit);
  60. line->set_anchor(MARGIN_RIGHT, ANCHOR_END);
  61. line->set_begin(Point2(15, 22));
  62. line->set_end(Point2(15, 35));
  63. add_child(line);
  64. register_text_enter(line);
  65. text_editor = NULL;
  66. set_hide_on_ok(false);
  67. }
  68. void FindReplaceBar::_notification(int p_what) {
  69. if (p_what == NOTIFICATION_READY) {
  70. find_prev->set_icon(get_icon("MoveUp", "EditorIcons"));
  71. find_next->set_icon(get_icon("MoveDown", "EditorIcons"));
  72. hide_button->set_normal_texture(get_icon("Close", "EditorIcons"));
  73. hide_button->set_hover_texture(get_icon("CloseHover", "EditorIcons"));
  74. hide_button->set_pressed_texture(get_icon("Close", "EditorIcons"));
  75. } else if (p_what == NOTIFICATION_VISIBILITY_CHANGED) {
  76. set_process_unhandled_input(is_visible());
  77. }
  78. }
  79. void FindReplaceBar::_unhandled_input(const InputEvent &p_event) {
  80. if (p_event.type == InputEvent::KEY) {
  81. const InputEventKey &k = p_event.key;
  82. if (k.pressed && (text_edit->has_focus() || text_vbc->is_a_parent_of(get_focus_owner()))) {
  83. bool accepted = true;
  84. switch (k.scancode) {
  85. case KEY_ESCAPE: {
  86. _hide_bar();
  87. } break;
  88. default: {
  89. accepted = false;
  90. } break;
  91. }
  92. if (accepted) {
  93. accept_event();
  94. }
  95. }
  96. }
  97. }
  98. bool FindReplaceBar::_search(uint32_t p_flags, int p_from_line, int p_from_col) {
  99. int line, col;
  100. String text = get_search_text();
  101. bool found = text_edit->search(text, p_flags, p_from_line, p_from_col, line, col);
  102. if (found) {
  103. if (!preserve_cursor) {
  104. text_edit->cursor_set_line(line, false);
  105. text_edit->cursor_set_column(col + text.length(), false);
  106. text_edit->center_viewport_to_cursor();
  107. }
  108. text_edit->set_search_text(text);
  109. text_edit->set_search_flags(p_flags);
  110. text_edit->set_current_search_result(line, col);
  111. result_line = line;
  112. result_col = col;
  113. set_error("");
  114. } else {
  115. result_line = -1;
  116. result_col = -1;
  117. text_edit->set_search_text("");
  118. set_error(text.empty() ? "" : TTR("No Matches"));
  119. }
  120. return found;
  121. }
  122. void FindReplaceBar::_replace() {
  123. if (result_line != -1 && result_col != -1) {
  124. text_edit->begin_complex_operation();
  125. text_edit->select(result_line, result_col, result_line, result_col + get_search_text().length());
  126. text_edit->insert_text_at_cursor(get_replace_text());
  127. text_edit->end_complex_operation();
  128. }
  129. search_current();
  130. }
  131. void FindReplaceBar::_replace_all() {
  132. // line as x so it gets priority in comparison, column as y
  133. Point2i orig_cursor(text_edit->cursor_get_line(), text_edit->cursor_get_column());
  134. Point2i prev_match = Point2(-1, -1);
  135. bool selection_enabled = text_edit->is_selection_active();
  136. Point2i selection_begin, selection_end;
  137. if (selection_enabled) {
  138. selection_begin = Point2i(text_edit->get_selection_from_line(), text_edit->get_selection_from_column());
  139. selection_end = Point2i(text_edit->get_selection_to_line(), text_edit->get_selection_to_column());
  140. }
  141. int vsval = text_edit->get_v_scroll();
  142. text_edit->cursor_set_line(0);
  143. text_edit->cursor_set_column(0);
  144. String replace_text = get_replace_text();
  145. int search_text_len = get_search_text().length();
  146. int rc = 0;
  147. replace_all_mode = true;
  148. text_edit->begin_complex_operation();
  149. while (search_next()) {
  150. // replace area
  151. Point2i match_from(result_line, result_col);
  152. Point2i match_to(result_line, result_col + search_text_len);
  153. if (match_from < prev_match)
  154. break; // done
  155. prev_match = Point2i(result_line, result_col + replace_text.length());
  156. text_edit->select(result_line, result_col, result_line, match_to.y);
  157. if (selection_enabled && is_selection_only()) {
  158. if (match_from < selection_begin || match_to > selection_end)
  159. continue;
  160. // replace but adjust selection bounds
  161. text_edit->insert_text_at_cursor(replace_text);
  162. if (match_to.x == selection_end.x)
  163. selection_end.y += replace_text.length() - search_text_len;
  164. } else {
  165. // just replace
  166. text_edit->insert_text_at_cursor(replace_text);
  167. }
  168. rc++;
  169. }
  170. text_edit->end_complex_operation();
  171. replace_all_mode = false;
  172. // restore editor state (selection, cursor, scroll)
  173. text_edit->cursor_set_line(orig_cursor.x);
  174. text_edit->cursor_set_column(orig_cursor.y);
  175. if (selection_enabled && is_selection_only()) {
  176. // reselect
  177. text_edit->select(selection_begin.x, selection_begin.y, selection_end.x, selection_end.y);
  178. } else {
  179. text_edit->deselect();
  180. }
  181. text_edit->set_v_scroll(vsval);
  182. set_error(vformat(TTR("Replaced %d Ocurrence(s)."), rc));
  183. }
  184. void FindReplaceBar::_get_search_from(int &r_line, int &r_col) {
  185. r_line = text_edit->cursor_get_line();
  186. r_col = text_edit->cursor_get_column();
  187. if (text_edit->is_selection_active() && !replace_all_mode) {
  188. int selection_line = text_edit->get_selection_from_line();
  189. if (text_edit->get_selection_text() == get_search_text() && r_line == selection_line) {
  190. int selection_from_col = text_edit->get_selection_from_column();
  191. if (r_col >= selection_from_col && r_col <= text_edit->get_selection_to_column()) {
  192. r_col = selection_line;
  193. r_col = selection_from_col;
  194. }
  195. }
  196. }
  197. if (r_line == result_line && r_col >= result_col && r_col <= result_col + get_search_text().length()) {
  198. r_col = result_col;
  199. }
  200. }
  201. bool FindReplaceBar::search_current() {
  202. uint32_t flags = 0;
  203. if (is_whole_words())
  204. flags |= TextEdit::SEARCH_WHOLE_WORDS;
  205. if (is_case_sensitive())
  206. flags |= TextEdit::SEARCH_MATCH_CASE;
  207. int line, col;
  208. _get_search_from(line, col);
  209. return _search(flags, line, col);
  210. }
  211. bool FindReplaceBar::search_prev() {
  212. uint32_t flags = 0;
  213. String text = get_search_text();
  214. if (is_whole_words())
  215. flags |= TextEdit::SEARCH_WHOLE_WORDS;
  216. if (is_case_sensitive())
  217. flags |= TextEdit::SEARCH_MATCH_CASE;
  218. flags |= TextEdit::SEARCH_BACKWARDS;
  219. int line, col;
  220. _get_search_from(line, col);
  221. col -= text.length();
  222. if (col < 0) {
  223. line -= 1;
  224. if (line < 0)
  225. line = text_edit->get_line_count() - 1;
  226. col = text_edit->get_line(line).length();
  227. }
  228. return _search(flags, line, col);
  229. }
  230. bool FindReplaceBar::search_next() {
  231. uint32_t flags = 0;
  232. String text = get_search_text();
  233. if (is_whole_words())
  234. flags |= TextEdit::SEARCH_WHOLE_WORDS;
  235. if (is_case_sensitive())
  236. flags |= TextEdit::SEARCH_MATCH_CASE;
  237. int line, col;
  238. _get_search_from(line, col);
  239. if (line == result_line && col == result_col) {
  240. col += text.length();
  241. if (col > text_edit->get_line(line).length()) {
  242. line += 1;
  243. if (line >= text_edit->get_line_count())
  244. line = 0;
  245. col = 0;
  246. }
  247. }
  248. return _search(flags, line, col);
  249. }
  250. void FindReplaceBar::_hide_bar() {
  251. if (replace_text->has_focus() || search_text->has_focus())
  252. text_edit->grab_focus();
  253. text_edit->set_search_text("");
  254. result_line = -1;
  255. result_col = -1;
  256. replace_hbc->hide();
  257. replace_options_hbc->hide();
  258. hide();
  259. }
  260. void FindReplaceBar::_show_search() {
  261. show();
  262. search_text->grab_focus();
  263. if (text_edit->is_selection_active()) {
  264. search_text->set_text(text_edit->get_selection_text());
  265. }
  266. if (!get_search_text().empty()) {
  267. search_text->select_all();
  268. search_text->set_cursor_pos(search_text->get_text().length());
  269. search_current();
  270. }
  271. }
  272. void FindReplaceBar::popup_search() {
  273. replace_hbc->hide();
  274. replace_options_hbc->hide();
  275. _show_search();
  276. }
  277. void FindReplaceBar::popup_replace() {
  278. if (!replace_hbc->is_visible() || !replace_options_hbc->is_visible()) {
  279. replace_text->clear();
  280. replace_hbc->show();
  281. replace_options_hbc->show();
  282. }
  283. _show_search();
  284. }
  285. void FindReplaceBar::_search_options_changed(bool p_pressed) {
  286. search_current();
  287. }
  288. void FindReplaceBar::_editor_text_changed() {
  289. if (is_visible()) {
  290. preserve_cursor = true;
  291. search_current();
  292. preserve_cursor = false;
  293. }
  294. }
  295. void FindReplaceBar::_search_text_changed(const String &p_text) {
  296. search_current();
  297. }
  298. void FindReplaceBar::_search_text_entered(const String &p_text) {
  299. search_next();
  300. }
  301. String FindReplaceBar::get_search_text() const {
  302. return search_text->get_text();
  303. }
  304. String FindReplaceBar::get_replace_text() const {
  305. return replace_text->get_text();
  306. }
  307. bool FindReplaceBar::is_case_sensitive() const {
  308. return case_sensitive->is_pressed();
  309. }
  310. bool FindReplaceBar::is_whole_words() const {
  311. return whole_words->is_pressed();
  312. }
  313. bool FindReplaceBar::is_selection_only() const {
  314. return selection_only->is_pressed();
  315. }
  316. void FindReplaceBar::set_error(const String &p_label) {
  317. error_label->set_text(p_label);
  318. }
  319. void FindReplaceBar::set_text_edit(TextEdit *p_text_edit) {
  320. text_edit = p_text_edit;
  321. text_edit->connect("text_changed", this, "_editor_text_changed");
  322. }
  323. void FindReplaceBar::_bind_methods() {
  324. ObjectTypeDB::bind_method("_unhandled_input", &FindReplaceBar::_unhandled_input);
  325. ObjectTypeDB::bind_method("_editor_text_changed", &FindReplaceBar::_editor_text_changed);
  326. ObjectTypeDB::bind_method("_search_text_changed", &FindReplaceBar::_search_text_changed);
  327. ObjectTypeDB::bind_method("_search_text_entered", &FindReplaceBar::_search_text_entered);
  328. ObjectTypeDB::bind_method("_search_current", &FindReplaceBar::search_current);
  329. ObjectTypeDB::bind_method("_search_next", &FindReplaceBar::search_next);
  330. ObjectTypeDB::bind_method("_search_prev", &FindReplaceBar::search_prev);
  331. ObjectTypeDB::bind_method("_replace_pressed", &FindReplaceBar::_replace);
  332. ObjectTypeDB::bind_method("_replace_all_pressed", &FindReplaceBar::_replace_all);
  333. ObjectTypeDB::bind_method("_search_options_changed", &FindReplaceBar::_search_options_changed);
  334. ObjectTypeDB::bind_method("_hide_pressed", &FindReplaceBar::_hide_bar);
  335. ADD_SIGNAL(MethodInfo("search"));
  336. }
  337. FindReplaceBar::FindReplaceBar() {
  338. replace_all_mode = false;
  339. preserve_cursor = false;
  340. text_vbc = memnew(VBoxContainer);
  341. add_child(text_vbc);
  342. HBoxContainer *search_hbc = memnew(HBoxContainer);
  343. text_vbc->add_child(search_hbc);
  344. search_text = memnew(LineEdit);
  345. search_hbc->add_child(search_text);
  346. search_text->set_custom_minimum_size(Size2(200, 0));
  347. search_text->connect("text_changed", this, "_search_text_changed");
  348. search_text->connect("text_entered", this, "_search_text_entered");
  349. find_prev = memnew(ToolButton);
  350. search_hbc->add_child(find_prev);
  351. find_prev->set_focus_mode(FOCUS_NONE);
  352. find_prev->connect("pressed", this, "_search_prev");
  353. find_next = memnew(ToolButton);
  354. search_hbc->add_child(find_next);
  355. find_next->set_focus_mode(FOCUS_NONE);
  356. find_next->connect("pressed", this, "_search_next");
  357. replace_hbc = memnew(HBoxContainer);
  358. text_vbc->add_child(replace_hbc);
  359. replace_hbc->hide();
  360. replace_text = memnew(LineEdit);
  361. replace_hbc->add_child(replace_text);
  362. replace_text->set_custom_minimum_size(Size2(200, 0));
  363. replace_text->connect("text_entered", this, "_search_text_entered");
  364. replace = memnew(ToolButton);
  365. replace_hbc->add_child(replace);
  366. replace->set_text(TTR("Replace"));
  367. replace->set_focus_mode(FOCUS_NONE);
  368. replace->connect("pressed", this, "_replace_pressed");
  369. replace_all = memnew(ToolButton);
  370. replace_hbc->add_child(replace_all);
  371. replace_all->set_text(TTR("Replace All"));
  372. replace_all->set_focus_mode(FOCUS_NONE);
  373. replace_all->connect("pressed", this, "_replace_all_pressed");
  374. Control *spacer_split = memnew(Control);
  375. spacer_split->set_custom_minimum_size(Size2(0, 1));
  376. text_vbc->add_child(spacer_split);
  377. VBoxContainer *options_vbc = memnew(VBoxContainer);
  378. add_child(options_vbc);
  379. options_vbc->set_h_size_flags(SIZE_EXPAND_FILL);
  380. HBoxContainer *search_options = memnew(HBoxContainer);
  381. options_vbc->add_child(search_options);
  382. case_sensitive = memnew(CheckBox);
  383. search_options->add_child(case_sensitive);
  384. case_sensitive->set_text(TTR("Match Case"));
  385. case_sensitive->set_focus_mode(FOCUS_NONE);
  386. case_sensitive->connect("toggled", this, "_search_options_changed");
  387. whole_words = memnew(CheckBox);
  388. search_options->add_child(whole_words);
  389. whole_words->set_text(TTR("Whole Words"));
  390. whole_words->set_focus_mode(FOCUS_NONE);
  391. whole_words->connect("toggled", this, "_search_options_changed");
  392. error_label = memnew(Label);
  393. search_options->add_child(error_label);
  394. error_label->add_color_override("font_color", Color(1, 1, 0, 1));
  395. error_label->add_color_override("font_color_shadow", Color(0, 0, 0, 1));
  396. error_label->add_constant_override("shadow_as_outline", 1);
  397. search_options->add_spacer();
  398. hide_button = memnew(TextureButton);
  399. search_options->add_child(hide_button);
  400. hide_button->set_focus_mode(FOCUS_NONE);
  401. hide_button->connect("pressed", this, "_hide_pressed");
  402. replace_options_hbc = memnew(HBoxContainer);
  403. options_vbc->add_child(replace_options_hbc);
  404. replace_options_hbc->hide();
  405. selection_only = memnew(CheckBox);
  406. replace_options_hbc->add_child(selection_only);
  407. selection_only->set_text(TTR("Selection Only"));
  408. selection_only->set_focus_mode(FOCUS_NONE);
  409. selection_only->connect("toggled", this, "_search_options_changed");
  410. }
  411. void FindReplaceDialog::popup_search() {
  412. set_title(TTR("Search"));
  413. replace_mc->hide();
  414. replace_label->hide();
  415. replace_vb->hide();
  416. skip->hide();
  417. popup_centered(Point2(300, 190));
  418. get_ok()->set_text(TTR("Find"));
  419. search_text->grab_focus();
  420. if (text_edit->is_selection_active() && (text_edit->get_selection_from_line() == text_edit->get_selection_to_line())) {
  421. search_text->set_text(text_edit->get_selection_text());
  422. }
  423. search_text->select_all();
  424. error_label->set_text("");
  425. }
  426. void FindReplaceDialog::popup_replace() {
  427. set_title(TTR("Replace"));
  428. bool do_selection = (text_edit->is_selection_active() && text_edit->get_selection_from_line() < text_edit->get_selection_to_line());
  429. set_replace_selection_only(do_selection);
  430. if (!do_selection && text_edit->is_selection_active()) {
  431. search_text->set_text(text_edit->get_selection_text());
  432. }
  433. replace_mc->show();
  434. replace_label->show();
  435. replace_vb->show();
  436. popup_centered(Point2(300, 300));
  437. if (search_text->get_text() != "" && replace_text->get_text() == "") {
  438. search_text->select(0, 0);
  439. replace_text->grab_focus();
  440. } else {
  441. search_text->grab_focus();
  442. search_text->select_all();
  443. }
  444. error_label->set_text("");
  445. if (prompt->is_pressed()) {
  446. skip->show();
  447. get_ok()->set_text(TTR("Next"));
  448. selection_only->set_disabled(true);
  449. } else {
  450. skip->hide();
  451. get_ok()->set_text(TTR("Replace"));
  452. selection_only->set_disabled(false);
  453. }
  454. }
  455. void FindReplaceDialog::_search_callback() {
  456. if (is_replace_mode())
  457. _replace();
  458. else
  459. _search();
  460. }
  461. void FindReplaceDialog::_replace_skip_callback() {
  462. _search();
  463. }
  464. void FindReplaceDialog::_replace() {
  465. text_edit->begin_complex_operation();
  466. if (is_replace_all_mode()) {
  467. //line as x so it gets priority in comparison, column as y
  468. Point2i orig_cursor(text_edit->cursor_get_line(), text_edit->cursor_get_column());
  469. Point2i prev_match = Point2(-1, -1);
  470. bool selection_enabled = text_edit->is_selection_active();
  471. Point2i selection_begin, selection_end;
  472. if (selection_enabled) {
  473. selection_begin = Point2i(text_edit->get_selection_from_line(), text_edit->get_selection_from_column());
  474. selection_end = Point2i(text_edit->get_selection_to_line(), text_edit->get_selection_to_column());
  475. }
  476. int vsval = text_edit->get_v_scroll();
  477. //int hsval = text_edit->get_h_scroll();
  478. text_edit->cursor_set_line(0);
  479. text_edit->cursor_set_column(0);
  480. int rc = 0;
  481. while (_search()) {
  482. if (!text_edit->is_selection_active()) {
  483. //search selects
  484. break;
  485. }
  486. //replace area
  487. Point2i match_from(text_edit->get_selection_from_line(), text_edit->get_selection_from_column());
  488. Point2i match_to(text_edit->get_selection_to_line(), text_edit->get_selection_to_column());
  489. if (match_from < prev_match)
  490. break; //done
  491. prev_match = match_to;
  492. if (selection_enabled && is_replace_selection_only()) {
  493. if (match_from < selection_begin || match_to > selection_end)
  494. continue;
  495. //replace but adjust selection bounds
  496. text_edit->insert_text_at_cursor(get_replace_text());
  497. if (match_to.x == selection_end.x)
  498. selection_end.y += get_replace_text().length() - get_search_text().length();
  499. } else {
  500. //just replace
  501. text_edit->insert_text_at_cursor(get_replace_text());
  502. }
  503. rc++;
  504. }
  505. //restore editor state (selection, cursor, scroll)
  506. text_edit->cursor_set_line(orig_cursor.x);
  507. text_edit->cursor_set_column(orig_cursor.y);
  508. if (selection_enabled && is_replace_selection_only()) {
  509. //reselect
  510. text_edit->select(selection_begin.x, selection_begin.y, selection_end.x, selection_end.y);
  511. } else {
  512. text_edit->deselect();
  513. }
  514. text_edit->set_v_scroll(vsval);
  515. // text_edit->set_h_scroll(hsval);
  516. error_label->set_text(vformat(TTR("Replaced %d ocurrence(s)."), rc));
  517. //hide();
  518. } else {
  519. if (text_edit->get_selection_text() == get_search_text()) {
  520. text_edit->insert_text_at_cursor(get_replace_text());
  521. }
  522. _search();
  523. }
  524. text_edit->end_complex_operation();
  525. }
  526. bool FindReplaceDialog::_search() {
  527. String text = get_search_text();
  528. uint32_t flags = 0;
  529. if (is_whole_words())
  530. flags |= TextEdit::SEARCH_WHOLE_WORDS;
  531. if (is_case_sensitive())
  532. flags |= TextEdit::SEARCH_MATCH_CASE;
  533. if (is_backwards())
  534. flags |= TextEdit::SEARCH_BACKWARDS;
  535. int line = text_edit->cursor_get_line(), col = text_edit->cursor_get_column();
  536. if (is_backwards()) {
  537. col -= 1;
  538. if (col < 0) {
  539. line -= 1;
  540. if (line < 0) {
  541. line = text_edit->get_line_count() - 1;
  542. }
  543. col = text_edit->get_line(line).length();
  544. }
  545. }
  546. bool found = text_edit->search(text, flags, line, col, line, col);
  547. if (found) {
  548. // print_line("found");
  549. text_edit->cursor_set_line(line);
  550. if (is_backwards())
  551. text_edit->cursor_set_column(col);
  552. else
  553. text_edit->cursor_set_column(col + text.length());
  554. text_edit->select(line, col, line, col + text.length());
  555. set_error("");
  556. return true;
  557. } else {
  558. set_error(TTR("Not found!"));
  559. return false;
  560. }
  561. }
  562. void FindReplaceDialog::_prompt_changed() {
  563. if (prompt->is_pressed()) {
  564. skip->show();
  565. get_ok()->set_text(TTR("Next"));
  566. selection_only->set_disabled(true);
  567. } else {
  568. skip->hide();
  569. get_ok()->set_text(TTR("Replace"));
  570. selection_only->set_disabled(false);
  571. }
  572. }
  573. void FindReplaceDialog::_skip_pressed() {
  574. _replace_skip_callback();
  575. }
  576. bool FindReplaceDialog::is_replace_mode() const {
  577. return replace_text->is_visible();
  578. }
  579. bool FindReplaceDialog::is_replace_all_mode() const {
  580. return !prompt->is_pressed();
  581. }
  582. bool FindReplaceDialog::is_replace_selection_only() const {
  583. return selection_only->is_pressed();
  584. }
  585. void FindReplaceDialog::set_replace_selection_only(bool p_enable) {
  586. selection_only->set_pressed(p_enable);
  587. }
  588. void FindReplaceDialog::ok_pressed() {
  589. _search_callback();
  590. }
  591. void FindReplaceDialog::_search_text_entered(const String &p_text) {
  592. if (replace_text->is_visible())
  593. return;
  594. emit_signal("search");
  595. _search();
  596. }
  597. void FindReplaceDialog::_replace_text_entered(const String &p_text) {
  598. if (!replace_text->is_visible())
  599. return;
  600. emit_signal("search");
  601. _replace();
  602. }
  603. String FindReplaceDialog::get_search_text() const {
  604. return search_text->get_text();
  605. }
  606. String FindReplaceDialog::get_replace_text() const {
  607. return replace_text->get_text();
  608. }
  609. bool FindReplaceDialog::is_whole_words() const {
  610. return whole_words->is_pressed();
  611. }
  612. bool FindReplaceDialog::is_case_sensitive() const {
  613. return case_sensitive->is_pressed();
  614. }
  615. bool FindReplaceDialog::is_backwards() const {
  616. return backwards->is_pressed();
  617. }
  618. void FindReplaceDialog::set_error(const String &p_error) {
  619. error_label->set_text(p_error);
  620. }
  621. void FindReplaceDialog::set_text_edit(TextEdit *p_text_edit) {
  622. text_edit = p_text_edit;
  623. }
  624. void FindReplaceDialog::search_next() {
  625. _search();
  626. }
  627. void FindReplaceDialog::_bind_methods() {
  628. ObjectTypeDB::bind_method("_search_text_entered", &FindReplaceDialog::_search_text_entered);
  629. ObjectTypeDB::bind_method("_replace_text_entered", &FindReplaceDialog::_replace_text_entered);
  630. ObjectTypeDB::bind_method("_prompt_changed", &FindReplaceDialog::_prompt_changed);
  631. ObjectTypeDB::bind_method("_skip_pressed", &FindReplaceDialog::_skip_pressed);
  632. ADD_SIGNAL(MethodInfo("search"));
  633. ADD_SIGNAL(MethodInfo("skip"));
  634. }
  635. FindReplaceDialog::FindReplaceDialog() {
  636. set_self_opacity(0.8);
  637. VBoxContainer *vb = memnew(VBoxContainer);
  638. add_child(vb);
  639. set_child_rect(vb);
  640. search_text = memnew(LineEdit);
  641. vb->add_margin_child(TTR("Search"), search_text);
  642. search_text->connect("text_entered", this, "_search_text_entered");
  643. //search_text->set_self_opacity(0.7);
  644. replace_label = memnew(Label);
  645. replace_label->set_text(TTR("Replace By"));
  646. vb->add_child(replace_label);
  647. replace_mc = memnew(MarginContainer);
  648. vb->add_child(replace_mc);
  649. replace_text = memnew(LineEdit);
  650. replace_text->set_anchor(MARGIN_RIGHT, ANCHOR_END);
  651. replace_text->set_begin(Point2(15, 132));
  652. replace_text->set_end(Point2(15, 135));
  653. //replace_text->set_self_opacity(0.7);
  654. replace_mc->add_child(replace_text);
  655. replace_text->connect("text_entered", this, "_replace_text_entered");
  656. MarginContainer *opt_mg = memnew(MarginContainer);
  657. vb->add_child(opt_mg);
  658. VBoxContainer *svb = memnew(VBoxContainer);
  659. opt_mg->add_child(svb);
  660. svb->add_child(memnew(Label));
  661. whole_words = memnew(CheckButton);
  662. whole_words->set_text(TTR("Whole Words"));
  663. svb->add_child(whole_words);
  664. case_sensitive = memnew(CheckButton);
  665. case_sensitive->set_text(TTR("Case Sensitive"));
  666. svb->add_child(case_sensitive);
  667. backwards = memnew(CheckButton);
  668. backwards->set_text(TTR("Backwards"));
  669. svb->add_child(backwards);
  670. opt_mg = memnew(MarginContainer);
  671. vb->add_child(opt_mg);
  672. VBoxContainer *rvb = memnew(VBoxContainer);
  673. opt_mg->add_child(rvb);
  674. replace_vb = rvb;
  675. // rvb ->add_child(memnew(HSeparator));
  676. rvb->add_child(memnew(Label));
  677. prompt = memnew(CheckButton);
  678. prompt->set_text(TTR("Prompt On Replace"));
  679. rvb->add_child(prompt);
  680. prompt->connect("pressed", this, "_prompt_changed");
  681. selection_only = memnew(CheckButton);
  682. selection_only->set_text(TTR("Selection Only"));
  683. rvb->add_child(selection_only);
  684. int margin = get_constant("margin", "Dialogs");
  685. int button_margin = get_constant("button_margin", "Dialogs");
  686. skip = memnew(Button);
  687. skip->set_anchor(MARGIN_LEFT, ANCHOR_END);
  688. skip->set_anchor(MARGIN_TOP, ANCHOR_END);
  689. skip->set_anchor(MARGIN_RIGHT, ANCHOR_END);
  690. skip->set_anchor(MARGIN_BOTTOM, ANCHOR_END);
  691. skip->set_begin(Point2(70, button_margin));
  692. skip->set_end(Point2(10, margin));
  693. skip->set_text(TTR("Skip"));
  694. add_child(skip);
  695. skip->connect("pressed", this, "_skip_pressed");
  696. error_label = memnew(Label);
  697. error_label->set_align(Label::ALIGN_CENTER);
  698. error_label->add_color_override("font_color", Color(1, 0.4, 0.3));
  699. error_label->add_color_override("font_color_shadow", Color(0, 0, 0, 0.2));
  700. error_label->add_constant_override("shadow_as_outline", 1);
  701. vb->add_child(error_label);
  702. set_hide_on_ok(false);
  703. }
  704. /*** CODE EDITOR ****/
  705. void CodeTextEditor::_text_editor_input_event(const InputEvent &p_event) {
  706. if (p_event.type == InputEvent::MOUSE_BUTTON) {
  707. const InputEventMouseButton &mb = p_event.mouse_button;
  708. if (mb.pressed && mb.mod.command) {
  709. if (mb.button_index == BUTTON_WHEEL_UP) {
  710. _zoom_in();
  711. } else if (mb.button_index == BUTTON_WHEEL_DOWN) {
  712. _zoom_out();
  713. }
  714. }
  715. } else if (p_event.type == InputEvent::KEY) {
  716. if (p_event.key.pressed) {
  717. if (ED_IS_SHORTCUT("script_editor/zoom_in", p_event)) {
  718. _zoom_in();
  719. }
  720. if (ED_IS_SHORTCUT("script_editor/zoom_out", p_event)) {
  721. _zoom_out();
  722. }
  723. if (ED_IS_SHORTCUT("script_editor/reset_zoom", p_event)) {
  724. _reset_zoom();
  725. }
  726. }
  727. }
  728. }
  729. void CodeTextEditor::_zoom_in() {
  730. font_resize_val += 1;
  731. if (font_resize_timer->get_time_left() == 0)
  732. font_resize_timer->start();
  733. }
  734. void CodeTextEditor::_zoom_out() {
  735. font_resize_val -= 1;
  736. if (font_resize_timer->get_time_left() == 0)
  737. font_resize_timer->start();
  738. }
  739. void CodeTextEditor::_reset_zoom() {
  740. Ref<DynamicFont> font = text_editor->get_font("font"); // reset source font size to default
  741. if (font.is_valid()) {
  742. EditorSettings::get_singleton()->set("global/source_font_size", 14);
  743. font->set_size(14);
  744. }
  745. }
  746. void CodeTextEditor::_line_col_changed() {
  747. line_nb->set_text(itos(text_editor->cursor_get_line() + 1));
  748. col_nb->set_text(itos(text_editor->cursor_get_column() + 1));
  749. }
  750. void CodeTextEditor::_text_changed() {
  751. code_complete_timer->start();
  752. idle->start();
  753. }
  754. void CodeTextEditor::_code_complete_timer_timeout() {
  755. if (!is_visible())
  756. return;
  757. if (enable_complete_timer)
  758. text_editor->query_code_comple();
  759. }
  760. void CodeTextEditor::_complete_request() {
  761. List<String> entries;
  762. _code_complete_script(text_editor->get_text_for_completion(), &entries);
  763. // print_line("COMPLETE: "+p_request);
  764. if (entries.size() == 0)
  765. return;
  766. Vector<String> strs;
  767. strs.resize(entries.size());
  768. int i = 0;
  769. for (List<String>::Element *E = entries.front(); E; E = E->next()) {
  770. strs[i++] = E->get();
  771. }
  772. text_editor->code_complete(strs);
  773. }
  774. void CodeTextEditor::_font_resize_timeout() {
  775. Ref<DynamicFont> font = text_editor->get_font("font");
  776. if (font.is_valid()) {
  777. int size = font->get_size() + font_resize_val;
  778. if (size >= 8 && size <= 96) {
  779. EditorSettings::get_singleton()->set("global/source_font_size", size);
  780. font->set_size(size);
  781. }
  782. font_resize_val = 0;
  783. }
  784. }
  785. void CodeTextEditor::update_editor_settings() {
  786. text_editor->set_auto_brace_completion(EditorSettings::get_singleton()->get("text_editor/auto_brace_complete"));
  787. text_editor->set_scroll_pass_end_of_file(EditorSettings::get_singleton()->get("text_editor/scroll_past_end_of_file"));
  788. text_editor->set_tab_size(EditorSettings::get_singleton()->get("text_editor/tab_size"));
  789. text_editor->set_draw_tabs(EditorSettings::get_singleton()->get("text_editor/draw_tabs"));
  790. text_editor->set_show_line_numbers(EditorSettings::get_singleton()->get("text_editor/show_line_numbers"));
  791. text_editor->set_line_numbers_zero_padded(EditorSettings::get_singleton()->get("text_editor/line_numbers_zero_padded"));
  792. text_editor->set_show_line_length_guideline(EditorSettings::get_singleton()->get("text_editor/show_line_length_guideline"));
  793. text_editor->set_line_length_guideline_column(EditorSettings::get_singleton()->get("text_editor/line_length_guideline_column"));
  794. text_editor->set_syntax_coloring(EditorSettings::get_singleton()->get("text_editor/syntax_highlighting"));
  795. text_editor->set_highlight_all_occurrences(EditorSettings::get_singleton()->get("text_editor/highlight_all_occurrences"));
  796. text_editor->cursor_set_blink_enabled(EditorSettings::get_singleton()->get("text_editor/caret_blink"));
  797. text_editor->cursor_set_blink_speed(EditorSettings::get_singleton()->get("text_editor/caret_blink_speed"));
  798. text_editor->set_draw_breakpoint_gutter(EditorSettings::get_singleton()->get("text_editor/show_breakpoint_gutter"));
  799. text_editor->cursor_set_block_mode(EditorSettings::get_singleton()->get("text_editor/block_caret"));
  800. }
  801. void CodeTextEditor::set_error(const String &p_error) {
  802. if (p_error != "") {
  803. error->set_text(p_error);
  804. error->show();
  805. } else {
  806. error->hide();
  807. }
  808. }
  809. void CodeTextEditor::_update_font() {
  810. // FONTS
  811. String editor_font = EDITOR_DEF("text_editor/font", "");
  812. bool font_overridden = false;
  813. if (editor_font != "") {
  814. Ref<Font> fnt = ResourceLoader::load(editor_font);
  815. if (fnt.is_valid()) {
  816. text_editor->add_font_override("font", fnt);
  817. font_overridden = true;
  818. }
  819. }
  820. if (!font_overridden)
  821. text_editor->add_font_override("font", get_font("source", "EditorFonts"));
  822. }
  823. void CodeTextEditor::_on_settings_change() {
  824. _update_font();
  825. // AUTO BRACE COMPLETION
  826. text_editor->set_auto_brace_completion(
  827. EDITOR_DEF("text_editor/auto_brace_complete", true));
  828. code_complete_timer->set_wait_time(
  829. EDITOR_DEF("text_editor/code_complete_delay", .3f));
  830. enable_complete_timer = EDITOR_DEF("text_editor/enable_code_completion_delay", true);
  831. // call hint settings
  832. text_editor->set_callhint_settings(
  833. EDITOR_DEF("text_editor/put_callhint_tooltip_below_current_line", true),
  834. EDITOR_DEF("text_editor/callhint_tooltip_offset", Vector2()));
  835. }
  836. void CodeTextEditor::_text_changed_idle_timeout() {
  837. _validate_script();
  838. }
  839. void CodeTextEditor::_notification(int p_what) {
  840. if (p_what == EditorSettings::NOTIFICATION_EDITOR_SETTINGS_CHANGED)
  841. _load_theme_settings();
  842. if (p_what == NOTIFICATION_ENTER_TREE) {
  843. _update_font();
  844. }
  845. }
  846. void CodeTextEditor::_bind_methods() {
  847. ObjectTypeDB::bind_method("_text_editor_input_event", &CodeTextEditor::_text_editor_input_event);
  848. ObjectTypeDB::bind_method("_line_col_changed", &CodeTextEditor::_line_col_changed);
  849. ObjectTypeDB::bind_method("_text_changed", &CodeTextEditor::_text_changed);
  850. ObjectTypeDB::bind_method("_on_settings_change", &CodeTextEditor::_on_settings_change);
  851. ObjectTypeDB::bind_method("_text_changed_idle_timeout", &CodeTextEditor::_text_changed_idle_timeout);
  852. ObjectTypeDB::bind_method("_code_complete_timer_timeout", &CodeTextEditor::_code_complete_timer_timeout);
  853. ObjectTypeDB::bind_method("_complete_request", &CodeTextEditor::_complete_request);
  854. ObjectTypeDB::bind_method("_font_resize_timeout", &CodeTextEditor::_font_resize_timeout);
  855. }
  856. CodeTextEditor::CodeTextEditor() {
  857. ED_SHORTCUT("script_editor/zoom_in", TTR("Zoom In"), KEY_MASK_CMD | KEY_EQUAL);
  858. ED_SHORTCUT("script_editor/zoom_out", TTR("Zoom Out"), KEY_MASK_CMD | KEY_MINUS);
  859. ED_SHORTCUT("script_editor/reset_zoom", TTR("Reset Zoom"), KEY_MASK_CMD | KEY_0);
  860. find_replace_bar = memnew(FindReplaceBar);
  861. add_child(find_replace_bar);
  862. find_replace_bar->set_h_size_flags(SIZE_EXPAND_FILL);
  863. find_replace_bar->hide();
  864. text_editor = memnew(TextEdit);
  865. add_child(text_editor);
  866. text_editor->set_v_size_flags(SIZE_EXPAND_FILL);
  867. find_replace_bar->set_text_edit(text_editor);
  868. text_editor->set_show_line_numbers(true);
  869. text_editor->set_brace_matching(true);
  870. text_editor->set_auto_indent(true);
  871. MarginContainer *status_mc = memnew(MarginContainer);
  872. add_child(status_mc);
  873. status_mc->set("custom_constants/margin_left", 2);
  874. status_mc->set("custom_constants/margin_top", 5);
  875. status_mc->set("custom_constants/margin_right", 2);
  876. status_mc->set("custom_constants/margin_bottom", 1);
  877. HBoxContainer *status_bar = memnew(HBoxContainer);
  878. status_mc->add_child(status_bar);
  879. status_bar->set_h_size_flags(SIZE_EXPAND_FILL);
  880. status_bar->add_child(memnew(Label)); //to keep the height if the other labels are not visible
  881. idle = memnew(Timer);
  882. add_child(idle);
  883. idle->set_one_shot(true);
  884. idle->set_wait_time(EDITOR_DEF("text_editor/idle_parse_delay", 2));
  885. code_complete_timer = memnew(Timer);
  886. add_child(code_complete_timer);
  887. code_complete_timer->set_one_shot(true);
  888. enable_complete_timer = EDITOR_DEF("text_editor/enable_code_completion_delay", true);
  889. code_complete_timer->set_wait_time(EDITOR_DEF("text_editor/code_complete_delay", .3f));
  890. error = memnew(Label);
  891. status_bar->add_child(error);
  892. error->hide();
  893. error->set_valign(Label::VALIGN_CENTER);
  894. error->add_color_override("font_color", Color(1, 0.7, 0.6, 0.9));
  895. status_bar->add_spacer();
  896. Label *line_txt = memnew(Label);
  897. status_bar->add_child(line_txt);
  898. line_txt->set_align(Label::ALIGN_RIGHT);
  899. line_txt->set_valign(Label::VALIGN_CENTER);
  900. line_txt->set_v_size_flags(SIZE_FILL);
  901. line_txt->set_text(TTR("Line:"));
  902. line_nb = memnew(Label);
  903. status_bar->add_child(line_nb);
  904. line_nb->set_valign(Label::VALIGN_CENTER);
  905. line_nb->set_v_size_flags(SIZE_FILL);
  906. line_nb->set_autowrap(true); // workaround to prevent resizing the label on each change
  907. line_nb->set_custom_minimum_size(Size2(40, 1) * EDSCALE);
  908. Label *col_txt = memnew(Label);
  909. status_bar->add_child(col_txt);
  910. col_txt->set_align(Label::ALIGN_RIGHT);
  911. col_txt->set_valign(Label::VALIGN_CENTER);
  912. col_txt->set_v_size_flags(SIZE_FILL);
  913. col_txt->set_text(TTR("Col:"));
  914. col_nb = memnew(Label);
  915. status_bar->add_child(col_nb);
  916. col_nb->set_valign(Label::VALIGN_CENTER);
  917. col_nb->set_v_size_flags(SIZE_FILL);
  918. col_nb->set_autowrap(true); // workaround to prevent resizing the label on each change
  919. col_nb->set_custom_minimum_size(Size2(40, 1) * EDSCALE);
  920. text_editor->connect("input_event", this, "_text_editor_input_event");
  921. text_editor->connect("cursor_changed", this, "_line_col_changed");
  922. text_editor->connect("text_changed", this, "_text_changed");
  923. text_editor->connect("request_completion", this, "_complete_request");
  924. Vector<String> cs;
  925. cs.push_back(".");
  926. cs.push_back(",");
  927. cs.push_back("(");
  928. text_editor->set_completion(true, cs);
  929. idle->connect("timeout", this, "_text_changed_idle_timeout");
  930. code_complete_timer->connect("timeout", this, "_code_complete_timer_timeout");
  931. font_resize_val = 0;
  932. font_resize_timer = memnew(Timer);
  933. add_child(font_resize_timer);
  934. font_resize_timer->set_one_shot(true);
  935. font_resize_timer->set_wait_time(0.07);
  936. font_resize_timer->connect("timeout", this, "_font_resize_timeout");
  937. EditorSettings::get_singleton()->connect("settings_changed", this, "_on_settings_change");
  938. }