code_editor.cpp 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277
  1. /*************************************************************************/
  2. /* code_editor.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2017 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_position(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_in_tree());
  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 occurrence(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() && !selection_only->is_pressed()) {
  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_in_tree() || !replace_options_hbc->is_visible_in_tree()) {
  279. replace_text->clear();
  280. replace_hbc->show();
  281. replace_options_hbc->show();
  282. }
  283. selection_only->set_pressed((text_edit->is_selection_active() && text_edit->get_selection_from_line() < text_edit->get_selection_to_line()));
  284. _show_search();
  285. }
  286. void FindReplaceBar::_search_options_changed(bool p_pressed) {
  287. search_current();
  288. }
  289. void FindReplaceBar::_editor_text_changed() {
  290. if (is_visible_in_tree()) {
  291. preserve_cursor = true;
  292. search_current();
  293. preserve_cursor = false;
  294. }
  295. }
  296. void FindReplaceBar::_search_text_changed(const String &p_text) {
  297. search_current();
  298. }
  299. void FindReplaceBar::_search_text_entered(const String &p_text) {
  300. search_next();
  301. }
  302. void FindReplaceBar::_replace_text_entered(const String &p_text) {
  303. if (selection_only->is_pressed() && text_edit->is_selection_active()) {
  304. _replace_all();
  305. _hide_bar();
  306. }
  307. }
  308. String FindReplaceBar::get_search_text() const {
  309. return search_text->get_text();
  310. }
  311. String FindReplaceBar::get_replace_text() const {
  312. return replace_text->get_text();
  313. }
  314. bool FindReplaceBar::is_case_sensitive() const {
  315. return case_sensitive->is_pressed();
  316. }
  317. bool FindReplaceBar::is_whole_words() const {
  318. return whole_words->is_pressed();
  319. }
  320. bool FindReplaceBar::is_selection_only() const {
  321. return selection_only->is_pressed();
  322. }
  323. void FindReplaceBar::set_error(const String &p_label) {
  324. error_label->set_text(p_label);
  325. }
  326. void FindReplaceBar::set_text_edit(TextEdit *p_text_edit) {
  327. text_edit = p_text_edit;
  328. text_edit->connect("text_changed", this, "_editor_text_changed");
  329. }
  330. void FindReplaceBar::_bind_methods() {
  331. ClassDB::bind_method("_unhandled_input", &FindReplaceBar::_unhandled_input);
  332. ClassDB::bind_method("_editor_text_changed", &FindReplaceBar::_editor_text_changed);
  333. ClassDB::bind_method("_search_text_changed", &FindReplaceBar::_search_text_changed);
  334. ClassDB::bind_method("_search_text_entered", &FindReplaceBar::_search_text_entered);
  335. ClassDB::bind_method("_replace_text_entered", &FindReplaceBar::_replace_text_entered);
  336. ClassDB::bind_method("_search_current", &FindReplaceBar::search_current);
  337. ClassDB::bind_method("_search_next", &FindReplaceBar::search_next);
  338. ClassDB::bind_method("_search_prev", &FindReplaceBar::search_prev);
  339. ClassDB::bind_method("_replace_pressed", &FindReplaceBar::_replace);
  340. ClassDB::bind_method("_replace_all_pressed", &FindReplaceBar::_replace_all);
  341. ClassDB::bind_method("_search_options_changed", &FindReplaceBar::_search_options_changed);
  342. ClassDB::bind_method("_hide_pressed", &FindReplaceBar::_hide_bar);
  343. ADD_SIGNAL(MethodInfo("search"));
  344. }
  345. FindReplaceBar::FindReplaceBar() {
  346. replace_all_mode = false;
  347. preserve_cursor = false;
  348. text_vbc = memnew(VBoxContainer);
  349. add_child(text_vbc);
  350. HBoxContainer *search_hbc = memnew(HBoxContainer);
  351. text_vbc->add_child(search_hbc);
  352. search_text = memnew(LineEdit);
  353. search_hbc->add_child(search_text);
  354. search_text->set_custom_minimum_size(Size2(200, 0));
  355. search_text->connect("text_changed", this, "_search_text_changed");
  356. search_text->connect("text_entered", this, "_search_text_entered");
  357. find_prev = memnew(ToolButton);
  358. search_hbc->add_child(find_prev);
  359. find_prev->set_focus_mode(FOCUS_NONE);
  360. find_prev->connect("pressed", this, "_search_prev");
  361. find_next = memnew(ToolButton);
  362. search_hbc->add_child(find_next);
  363. find_next->set_focus_mode(FOCUS_NONE);
  364. find_next->connect("pressed", this, "_search_next");
  365. replace_hbc = memnew(HBoxContainer);
  366. text_vbc->add_child(replace_hbc);
  367. replace_hbc->hide();
  368. replace_text = memnew(LineEdit);
  369. replace_hbc->add_child(replace_text);
  370. replace_text->set_custom_minimum_size(Size2(200, 0));
  371. replace_text->connect("text_entered", this, "_replace_text_entered");
  372. replace = memnew(Button);
  373. replace_hbc->add_child(replace);
  374. replace->set_text(TTR("Replace"));
  375. //replace->set_focus_mode(FOCUS_NONE);
  376. replace->connect("pressed", this, "_replace_pressed");
  377. replace_all = memnew(Button);
  378. replace_hbc->add_child(replace_all);
  379. replace_all->set_text(TTR("Replace All"));
  380. //replace_all->set_focus_mode(FOCUS_NONE);
  381. replace_all->connect("pressed", this, "_replace_all_pressed");
  382. Control *spacer_split = memnew(Control);
  383. spacer_split->set_custom_minimum_size(Size2(0, 1));
  384. text_vbc->add_child(spacer_split);
  385. VBoxContainer *options_vbc = memnew(VBoxContainer);
  386. add_child(options_vbc);
  387. options_vbc->set_h_size_flags(SIZE_EXPAND_FILL);
  388. HBoxContainer *search_options = memnew(HBoxContainer);
  389. options_vbc->add_child(search_options);
  390. case_sensitive = memnew(CheckBox);
  391. search_options->add_child(case_sensitive);
  392. case_sensitive->set_text(TTR("Match Case"));
  393. case_sensitive->set_focus_mode(FOCUS_NONE);
  394. case_sensitive->connect("toggled", this, "_search_options_changed");
  395. whole_words = memnew(CheckBox);
  396. search_options->add_child(whole_words);
  397. whole_words->set_text(TTR("Whole Words"));
  398. whole_words->set_focus_mode(FOCUS_NONE);
  399. whole_words->connect("toggled", this, "_search_options_changed");
  400. error_label = memnew(Label);
  401. search_options->add_child(error_label);
  402. error_label->add_color_override("font_color", Color(1, 1, 0, 1));
  403. error_label->add_color_override("font_color_shadow", Color(0, 0, 0, 1));
  404. error_label->add_constant_override("shadow_as_outline", 1);
  405. search_options->add_spacer();
  406. hide_button = memnew(TextureButton);
  407. search_options->add_child(hide_button);
  408. hide_button->set_focus_mode(FOCUS_NONE);
  409. hide_button->connect("pressed", this, "_hide_pressed");
  410. replace_options_hbc = memnew(HBoxContainer);
  411. options_vbc->add_child(replace_options_hbc);
  412. replace_options_hbc->hide();
  413. selection_only = memnew(CheckBox);
  414. replace_options_hbc->add_child(selection_only);
  415. selection_only->set_text(TTR("Selection Only"));
  416. selection_only->set_focus_mode(FOCUS_NONE);
  417. selection_only->connect("toggled", this, "_search_options_changed");
  418. }
  419. void FindReplaceDialog::popup_search() {
  420. set_title(TTR("Search"));
  421. replace_mc->hide();
  422. replace_label->hide();
  423. replace_vb->hide();
  424. skip->hide();
  425. popup_centered(Point2(300, 190));
  426. get_ok()->set_text(TTR("Find"));
  427. search_text->grab_focus();
  428. if (text_edit->is_selection_active() && (text_edit->get_selection_from_line() == text_edit->get_selection_to_line())) {
  429. search_text->set_text(text_edit->get_selection_text());
  430. }
  431. search_text->select_all();
  432. error_label->set_text("");
  433. }
  434. void FindReplaceDialog::popup_replace() {
  435. set_title(TTR("Replace"));
  436. bool do_selection = (text_edit->is_selection_active() && text_edit->get_selection_from_line() < text_edit->get_selection_to_line());
  437. set_replace_selection_only(do_selection);
  438. if (!do_selection && text_edit->is_selection_active()) {
  439. search_text->set_text(text_edit->get_selection_text());
  440. }
  441. replace_mc->show();
  442. replace_label->show();
  443. replace_vb->show();
  444. popup_centered(Point2(300, 300));
  445. if (search_text->get_text() != "" && replace_text->get_text() == "") {
  446. search_text->select(0, 0);
  447. replace_text->grab_focus();
  448. } else {
  449. search_text->grab_focus();
  450. search_text->select_all();
  451. }
  452. error_label->set_text("");
  453. if (prompt->is_pressed()) {
  454. skip->show();
  455. get_ok()->set_text(TTR("Next"));
  456. selection_only->set_disabled(true);
  457. } else {
  458. skip->hide();
  459. get_ok()->set_text(TTR("Replace"));
  460. selection_only->set_disabled(false);
  461. }
  462. }
  463. void FindReplaceDialog::_search_callback() {
  464. if (is_replace_mode())
  465. _replace();
  466. else
  467. _search();
  468. }
  469. void FindReplaceDialog::_replace_skip_callback() {
  470. _search();
  471. }
  472. void FindReplaceDialog::_replace() {
  473. text_edit->begin_complex_operation();
  474. if (is_replace_all_mode()) {
  475. //line as x so it gets priority in comparison, column as y
  476. Point2i orig_cursor(text_edit->cursor_get_line(), text_edit->cursor_get_column());
  477. Point2i prev_match = Point2(-1, -1);
  478. bool selection_enabled = text_edit->is_selection_active();
  479. Point2i selection_begin, selection_end;
  480. if (selection_enabled) {
  481. selection_begin = Point2i(text_edit->get_selection_from_line(), text_edit->get_selection_from_column());
  482. selection_end = Point2i(text_edit->get_selection_to_line(), text_edit->get_selection_to_column());
  483. }
  484. int vsval = text_edit->get_v_scroll();
  485. //int hsval = text_edit->get_h_scroll();
  486. text_edit->cursor_set_line(0);
  487. text_edit->cursor_set_column(0);
  488. int rc = 0;
  489. while (_search()) {
  490. if (!text_edit->is_selection_active()) {
  491. //search selects
  492. break;
  493. }
  494. //replace area
  495. Point2i match_from(text_edit->get_selection_from_line(), text_edit->get_selection_from_column());
  496. Point2i match_to(text_edit->get_selection_to_line(), text_edit->get_selection_to_column());
  497. if (match_from < prev_match)
  498. break; //done
  499. prev_match = match_to;
  500. if (selection_enabled && is_replace_selection_only()) {
  501. if (match_from < selection_begin || match_to > selection_end)
  502. continue;
  503. //replace but adjust selection bounds
  504. text_edit->insert_text_at_cursor(get_replace_text());
  505. if (match_to.x == selection_end.x)
  506. selection_end.y += get_replace_text().length() - get_search_text().length();
  507. } else {
  508. //just replace
  509. text_edit->insert_text_at_cursor(get_replace_text());
  510. }
  511. rc++;
  512. }
  513. //restore editor state (selection, cursor, scroll)
  514. text_edit->cursor_set_line(orig_cursor.x);
  515. text_edit->cursor_set_column(orig_cursor.y);
  516. if (selection_enabled && is_replace_selection_only()) {
  517. //reselect
  518. text_edit->select(selection_begin.x, selection_begin.y, selection_end.x, selection_end.y);
  519. } else {
  520. text_edit->deselect();
  521. }
  522. text_edit->set_v_scroll(vsval);
  523. //text_edit->set_h_scroll(hsval);
  524. error_label->set_text(vformat(TTR("Replaced %d occurrence(s)."), rc));
  525. //hide();
  526. } else {
  527. if (text_edit->get_selection_text() == get_search_text()) {
  528. text_edit->insert_text_at_cursor(get_replace_text());
  529. }
  530. _search();
  531. }
  532. text_edit->end_complex_operation();
  533. }
  534. bool FindReplaceDialog::_search() {
  535. String text = get_search_text();
  536. uint32_t flags = 0;
  537. if (is_whole_words())
  538. flags |= TextEdit::SEARCH_WHOLE_WORDS;
  539. if (is_case_sensitive())
  540. flags |= TextEdit::SEARCH_MATCH_CASE;
  541. if (is_backwards())
  542. flags |= TextEdit::SEARCH_BACKWARDS;
  543. int line = text_edit->cursor_get_line(), col = text_edit->cursor_get_column();
  544. if (is_backwards()) {
  545. col -= 1;
  546. if (col < 0) {
  547. line -= 1;
  548. if (line < 0) {
  549. line = text_edit->get_line_count() - 1;
  550. }
  551. col = text_edit->get_line(line).length();
  552. }
  553. }
  554. bool found = text_edit->search(text, flags, line, col, line, col);
  555. if (found) {
  556. // print_line("found");
  557. text_edit->cursor_set_line(line);
  558. if (is_backwards())
  559. text_edit->cursor_set_column(col);
  560. else
  561. text_edit->cursor_set_column(col + text.length());
  562. text_edit->select(line, col, line, col + text.length());
  563. set_error("");
  564. return true;
  565. } else {
  566. set_error(TTR("Not found!"));
  567. return false;
  568. }
  569. }
  570. void FindReplaceDialog::_prompt_changed() {
  571. if (prompt->is_pressed()) {
  572. skip->show();
  573. get_ok()->set_text(TTR("Next"));
  574. selection_only->set_disabled(true);
  575. } else {
  576. skip->hide();
  577. get_ok()->set_text(TTR("Replace"));
  578. selection_only->set_disabled(false);
  579. }
  580. }
  581. void FindReplaceDialog::_skip_pressed() {
  582. _replace_skip_callback();
  583. }
  584. bool FindReplaceDialog::is_replace_mode() const {
  585. return replace_text->is_visible_in_tree();
  586. }
  587. bool FindReplaceDialog::is_replace_all_mode() const {
  588. return !prompt->is_pressed();
  589. }
  590. bool FindReplaceDialog::is_replace_selection_only() const {
  591. return selection_only->is_pressed();
  592. }
  593. void FindReplaceDialog::set_replace_selection_only(bool p_enable) {
  594. selection_only->set_pressed(p_enable);
  595. }
  596. void FindReplaceDialog::ok_pressed() {
  597. _search_callback();
  598. }
  599. void FindReplaceDialog::_search_text_entered(const String &p_text) {
  600. if (replace_text->is_visible_in_tree())
  601. return;
  602. emit_signal("search");
  603. _search();
  604. }
  605. void FindReplaceDialog::_replace_text_entered(const String &p_text) {
  606. if (!replace_text->is_visible_in_tree())
  607. return;
  608. emit_signal("search");
  609. _replace();
  610. }
  611. String FindReplaceDialog::get_search_text() const {
  612. return search_text->get_text();
  613. }
  614. String FindReplaceDialog::get_replace_text() const {
  615. return replace_text->get_text();
  616. }
  617. bool FindReplaceDialog::is_whole_words() const {
  618. return whole_words->is_pressed();
  619. }
  620. bool FindReplaceDialog::is_case_sensitive() const {
  621. return case_sensitive->is_pressed();
  622. }
  623. bool FindReplaceDialog::is_backwards() const {
  624. return backwards->is_pressed();
  625. }
  626. void FindReplaceDialog::set_error(const String &p_error) {
  627. error_label->set_text(p_error);
  628. }
  629. void FindReplaceDialog::set_text_edit(TextEdit *p_text_edit) {
  630. text_edit = p_text_edit;
  631. }
  632. void FindReplaceDialog::search_next() {
  633. _search();
  634. }
  635. void FindReplaceDialog::_bind_methods() {
  636. ClassDB::bind_method("_search_text_entered", &FindReplaceDialog::_search_text_entered);
  637. ClassDB::bind_method("_replace_text_entered", &FindReplaceDialog::_replace_text_entered);
  638. ClassDB::bind_method("_prompt_changed", &FindReplaceDialog::_prompt_changed);
  639. ClassDB::bind_method("_skip_pressed", &FindReplaceDialog::_skip_pressed);
  640. ADD_SIGNAL(MethodInfo("search"));
  641. ADD_SIGNAL(MethodInfo("skip"));
  642. }
  643. FindReplaceDialog::FindReplaceDialog() {
  644. set_self_modulate(Color(1, 1, 1, 0.8));
  645. VBoxContainer *vb = memnew(VBoxContainer);
  646. add_child(vb);
  647. search_text = memnew(LineEdit);
  648. vb->add_margin_child(TTR("Search"), search_text);
  649. search_text->connect("text_entered", this, "_search_text_entered");
  650. //search_text->set_self_opacity(0.7);
  651. replace_label = memnew(Label);
  652. replace_label->set_text(TTR("Replace By"));
  653. vb->add_child(replace_label);
  654. replace_mc = memnew(MarginContainer);
  655. vb->add_child(replace_mc);
  656. replace_text = memnew(LineEdit);
  657. replace_text->set_anchor(MARGIN_RIGHT, ANCHOR_END);
  658. replace_text->set_begin(Point2(15, 132));
  659. replace_text->set_end(Point2(15, 135));
  660. //replace_text->set_self_opacity(0.7);
  661. replace_mc->add_child(replace_text);
  662. replace_text->connect("text_entered", this, "_replace_text_entered");
  663. MarginContainer *opt_mg = memnew(MarginContainer);
  664. vb->add_child(opt_mg);
  665. VBoxContainer *svb = memnew(VBoxContainer);
  666. opt_mg->add_child(svb);
  667. svb->add_child(memnew(Label));
  668. whole_words = memnew(CheckButton);
  669. whole_words->set_text(TTR("Whole Words"));
  670. svb->add_child(whole_words);
  671. case_sensitive = memnew(CheckButton);
  672. case_sensitive->set_text(TTR("Case Sensitive"));
  673. svb->add_child(case_sensitive);
  674. backwards = memnew(CheckButton);
  675. backwards->set_text(TTR("Backwards"));
  676. svb->add_child(backwards);
  677. opt_mg = memnew(MarginContainer);
  678. vb->add_child(opt_mg);
  679. VBoxContainer *rvb = memnew(VBoxContainer);
  680. opt_mg->add_child(rvb);
  681. replace_vb = rvb;
  682. //rvb ->add_child(memnew(HSeparator));
  683. rvb->add_child(memnew(Label));
  684. prompt = memnew(CheckButton);
  685. prompt->set_text(TTR("Prompt On Replace"));
  686. rvb->add_child(prompt);
  687. prompt->connect("pressed", this, "_prompt_changed");
  688. selection_only = memnew(CheckButton);
  689. selection_only->set_text(TTR("Selection Only"));
  690. rvb->add_child(selection_only);
  691. int margin = get_constant("margin", "Dialogs");
  692. int button_margin = get_constant("button_margin", "Dialogs");
  693. skip = memnew(Button);
  694. skip->set_anchor(MARGIN_LEFT, ANCHOR_END);
  695. skip->set_anchor(MARGIN_TOP, ANCHOR_END);
  696. skip->set_anchor(MARGIN_RIGHT, ANCHOR_END);
  697. skip->set_anchor(MARGIN_BOTTOM, ANCHOR_END);
  698. skip->set_begin(Point2(70, button_margin));
  699. skip->set_end(Point2(10, margin));
  700. skip->set_text(TTR("Skip"));
  701. add_child(skip);
  702. skip->connect("pressed", this, "_skip_pressed");
  703. error_label = memnew(Label);
  704. error_label->set_align(Label::ALIGN_CENTER);
  705. error_label->add_color_override("font_color", Color(1, 0.4, 0.3));
  706. error_label->add_color_override("font_color_shadow", Color(0, 0, 0, 0.2));
  707. error_label->add_constant_override("shadow_as_outline", 1);
  708. vb->add_child(error_label);
  709. set_hide_on_ok(false);
  710. }
  711. /*** CODE EDITOR ****/
  712. void CodeTextEditor::_text_editor_gui_input(const InputEvent &p_event) {
  713. if (p_event.type == InputEvent::MOUSE_BUTTON) {
  714. const InputEventMouseButton &mb = p_event.mouse_button;
  715. if (mb.pressed && mb.mod.command) {
  716. if (mb.button_index == BUTTON_WHEEL_UP) {
  717. _zoom_in();
  718. } else if (mb.button_index == BUTTON_WHEEL_DOWN) {
  719. _zoom_out();
  720. }
  721. }
  722. } else if (p_event.type == InputEvent::KEY) {
  723. if (p_event.key.pressed) {
  724. if (ED_IS_SHORTCUT("script_editor/zoom_in", p_event)) {
  725. _zoom_in();
  726. }
  727. if (ED_IS_SHORTCUT("script_editor/zoom_out", p_event)) {
  728. _zoom_out();
  729. }
  730. if (ED_IS_SHORTCUT("script_editor/reset_zoom", p_event)) {
  731. _reset_zoom();
  732. }
  733. }
  734. }
  735. }
  736. void CodeTextEditor::_zoom_in() {
  737. font_resize_val += 1;
  738. if (font_resize_timer->get_time_left() == 0)
  739. font_resize_timer->start();
  740. }
  741. void CodeTextEditor::_zoom_out() {
  742. font_resize_val -= 1;
  743. if (font_resize_timer->get_time_left() == 0)
  744. font_resize_timer->start();
  745. }
  746. void CodeTextEditor::_reset_zoom() {
  747. Ref<DynamicFont> font = text_editor->get_font("font"); // reset source font size to default
  748. if (font.is_valid()) {
  749. EditorSettings::get_singleton()->set("interface/source_font_size", 14);
  750. font->set_size(14);
  751. }
  752. }
  753. void CodeTextEditor::_line_col_changed() {
  754. line_nb->set_text(itos(text_editor->cursor_get_line() + 1));
  755. col_nb->set_text(itos(text_editor->cursor_get_column() + 1));
  756. }
  757. void CodeTextEditor::_text_changed() {
  758. code_complete_timer->start();
  759. idle->start();
  760. }
  761. void CodeTextEditor::_code_complete_timer_timeout() {
  762. if (!is_visible_in_tree())
  763. return;
  764. if (enable_complete_timer)
  765. text_editor->query_code_comple();
  766. }
  767. void CodeTextEditor::_complete_request() {
  768. List<String> entries;
  769. String ctext = text_editor->get_text_for_completion();
  770. _code_complete_script(ctext, &entries);
  771. if (code_complete_func) {
  772. code_complete_func(code_complete_ud, ctext, &entries);
  773. }
  774. // print_line("COMPLETE: "+p_request);
  775. if (entries.size() == 0)
  776. return;
  777. Vector<String> strs;
  778. strs.resize(entries.size());
  779. int i = 0;
  780. for (List<String>::Element *E = entries.front(); E; E = E->next()) {
  781. strs[i++] = E->get();
  782. }
  783. text_editor->code_complete(strs);
  784. }
  785. void CodeTextEditor::_font_resize_timeout() {
  786. Ref<DynamicFont> font = text_editor->get_font("font");
  787. if (font.is_valid()) {
  788. int size = font->get_size() + font_resize_val;
  789. if (size >= 8 && size <= 96) {
  790. EditorSettings::get_singleton()->set("interface/source_font_size", size);
  791. font->set_size(size);
  792. }
  793. font_resize_val = 0;
  794. }
  795. }
  796. void CodeTextEditor::update_editor_settings() {
  797. text_editor->set_auto_brace_completion(EditorSettings::get_singleton()->get("text_editor/completion/auto_brace_complete"));
  798. text_editor->set_scroll_pass_end_of_file(EditorSettings::get_singleton()->get("text_editor/cursor/scroll_past_end_of_file"));
  799. text_editor->set_indent_using_spaces(EditorSettings::get_singleton()->get("text_editor/indent/type"));
  800. text_editor->set_indent_size(EditorSettings::get_singleton()->get("text_editor/indent/size"));
  801. text_editor->set_draw_tabs(EditorSettings::get_singleton()->get("text_editor/indent/draw_tabs"));
  802. text_editor->set_show_line_numbers(EditorSettings::get_singleton()->get("text_editor/line_numbers/show_line_numbers"));
  803. text_editor->set_line_numbers_zero_padded(EditorSettings::get_singleton()->get("text_editor/line_numbers/line_numbers_zero_padded"));
  804. text_editor->set_show_line_length_guideline(EditorSettings::get_singleton()->get("text_editor/line_numbers/show_line_length_guideline"));
  805. text_editor->set_line_length_guideline_column(EditorSettings::get_singleton()->get("text_editor/line_numbers/line_length_guideline_column"));
  806. text_editor->set_syntax_coloring(EditorSettings::get_singleton()->get("text_editor/highlighting/syntax_highlighting"));
  807. text_editor->set_highlight_all_occurrences(EditorSettings::get_singleton()->get("text_editor/highlighting/highlight_all_occurrences"));
  808. text_editor->cursor_set_blink_enabled(EditorSettings::get_singleton()->get("text_editor/cursor/caret_blink"));
  809. text_editor->cursor_set_blink_speed(EditorSettings::get_singleton()->get("text_editor/cursor/caret_blink_speed"));
  810. text_editor->set_draw_breakpoint_gutter(EditorSettings::get_singleton()->get("text_editor/line_numbers/show_breakpoint_gutter"));
  811. text_editor->cursor_set_block_mode(EditorSettings::get_singleton()->get("text_editor/cursor/block_caret"));
  812. }
  813. void CodeTextEditor::set_error(const String &p_error) {
  814. if (p_error != "") {
  815. error->set_text(p_error);
  816. error->show();
  817. } else {
  818. error->hide();
  819. }
  820. }
  821. void CodeTextEditor::_update_font() {
  822. // FONTS
  823. String editor_font = EDITOR_DEF("text_editor/theme/font", "");
  824. bool font_overridden = false;
  825. if (editor_font != "") {
  826. Ref<Font> fnt = ResourceLoader::load(editor_font);
  827. if (fnt.is_valid()) {
  828. text_editor->add_font_override("font", fnt);
  829. font_overridden = true;
  830. }
  831. }
  832. if (!font_overridden) {
  833. text_editor->add_font_override("font", get_font("source", "EditorFonts"));
  834. }
  835. }
  836. void CodeTextEditor::_on_settings_change() {
  837. _update_font();
  838. // AUTO BRACE COMPLETION
  839. text_editor->set_auto_brace_completion(
  840. EDITOR_DEF("text_editor/completion/auto_brace_complete", true));
  841. code_complete_timer->set_wait_time(
  842. EDITOR_DEF("text_editor/completion/code_complete_delay", .3f));
  843. enable_complete_timer = EDITOR_DEF("text_editor/completion/enable_code_completion_delay", true);
  844. // call hint settings
  845. text_editor->set_callhint_settings(
  846. EDITOR_DEF("text_editor/completion/put_callhint_tooltip_below_current_line", true),
  847. EDITOR_DEF("text_editor/completion/callhint_tooltip_offset", Vector2()));
  848. }
  849. void CodeTextEditor::_text_changed_idle_timeout() {
  850. _validate_script();
  851. emit_signal("validate_script");
  852. }
  853. void CodeTextEditor::_notification(int p_what) {
  854. if (p_what == EditorSettings::NOTIFICATION_EDITOR_SETTINGS_CHANGED) {
  855. _load_theme_settings();
  856. emit_signal("load_theme_settings");
  857. }
  858. if (p_what == NOTIFICATION_THEME_CHANGED) {
  859. _update_font();
  860. }
  861. }
  862. void CodeTextEditor::_bind_methods() {
  863. ClassDB::bind_method("_text_editor_gui_input", &CodeTextEditor::_text_editor_gui_input);
  864. ClassDB::bind_method("_line_col_changed", &CodeTextEditor::_line_col_changed);
  865. ClassDB::bind_method("_text_changed", &CodeTextEditor::_text_changed);
  866. ClassDB::bind_method("_on_settings_change", &CodeTextEditor::_on_settings_change);
  867. ClassDB::bind_method("_text_changed_idle_timeout", &CodeTextEditor::_text_changed_idle_timeout);
  868. ClassDB::bind_method("_code_complete_timer_timeout", &CodeTextEditor::_code_complete_timer_timeout);
  869. ClassDB::bind_method("_complete_request", &CodeTextEditor::_complete_request);
  870. ClassDB::bind_method("_font_resize_timeout", &CodeTextEditor::_font_resize_timeout);
  871. ADD_SIGNAL(MethodInfo("validate_script"));
  872. ADD_SIGNAL(MethodInfo("load_theme_settings"));
  873. }
  874. void CodeTextEditor::set_code_complete_func(CodeTextEditorCodeCompleteFunc p_code_complete_func, void *p_ud) {
  875. code_complete_func = p_code_complete_func;
  876. code_complete_ud = p_ud;
  877. }
  878. CodeTextEditor::CodeTextEditor() {
  879. code_complete_func = NULL;
  880. ED_SHORTCUT("script_editor/zoom_in", TTR("Zoom In"), KEY_MASK_CMD | KEY_EQUAL);
  881. ED_SHORTCUT("script_editor/zoom_out", TTR("Zoom Out"), KEY_MASK_CMD | KEY_MINUS);
  882. ED_SHORTCUT("script_editor/reset_zoom", TTR("Reset Zoom"), KEY_MASK_CMD | KEY_0);
  883. find_replace_bar = memnew(FindReplaceBar);
  884. add_child(find_replace_bar);
  885. find_replace_bar->set_h_size_flags(SIZE_EXPAND_FILL);
  886. find_replace_bar->hide();
  887. text_editor = memnew(TextEdit);
  888. add_child(text_editor);
  889. text_editor->set_v_size_flags(SIZE_EXPAND_FILL);
  890. find_replace_bar->set_text_edit(text_editor);
  891. text_editor->set_show_line_numbers(true);
  892. text_editor->set_brace_matching(true);
  893. text_editor->set_auto_indent(true);
  894. MarginContainer *status_mc = memnew(MarginContainer);
  895. add_child(status_mc);
  896. status_mc->set("custom_constants/margin_left", 2);
  897. status_mc->set("custom_constants/margin_top", 5);
  898. status_mc->set("custom_constants/margin_right", 2);
  899. status_mc->set("custom_constants/margin_bottom", 1);
  900. HBoxContainer *status_bar = memnew(HBoxContainer);
  901. status_mc->add_child(status_bar);
  902. status_bar->set_h_size_flags(SIZE_EXPAND_FILL);
  903. status_bar->add_child(memnew(Label)); //to keep the height if the other labels are not visible
  904. idle = memnew(Timer);
  905. add_child(idle);
  906. idle->set_one_shot(true);
  907. idle->set_wait_time(EDITOR_DEF("text_editor/completion/idle_parse_delay", 2));
  908. code_complete_timer = memnew(Timer);
  909. add_child(code_complete_timer);
  910. code_complete_timer->set_one_shot(true);
  911. enable_complete_timer = EDITOR_DEF("text_editor/completion/enable_code_completion_delay", true);
  912. code_complete_timer->set_wait_time(EDITOR_DEF("text_editor/completion/code_complete_delay", .3f));
  913. error = memnew(Label);
  914. status_bar->add_child(error);
  915. error->hide();
  916. error->set_valign(Label::VALIGN_CENTER);
  917. error->add_color_override("font_color", Color(1, 0.7, 0.6, 0.9));
  918. status_bar->add_spacer();
  919. Label *line_txt = memnew(Label);
  920. status_bar->add_child(line_txt);
  921. line_txt->set_align(Label::ALIGN_RIGHT);
  922. line_txt->set_valign(Label::VALIGN_CENTER);
  923. line_txt->set_v_size_flags(SIZE_FILL);
  924. line_txt->set_text(TTR("Line:"));
  925. line_nb = memnew(Label);
  926. status_bar->add_child(line_nb);
  927. line_nb->set_valign(Label::VALIGN_CENTER);
  928. line_nb->set_v_size_flags(SIZE_FILL);
  929. line_nb->set_autowrap(true); // workaround to prevent resizing the label on each change
  930. line_nb->set_custom_minimum_size(Size2(40, 1) * EDSCALE);
  931. Label *col_txt = memnew(Label);
  932. status_bar->add_child(col_txt);
  933. col_txt->set_align(Label::ALIGN_RIGHT);
  934. col_txt->set_valign(Label::VALIGN_CENTER);
  935. col_txt->set_v_size_flags(SIZE_FILL);
  936. col_txt->set_text(TTR("Col:"));
  937. col_nb = memnew(Label);
  938. status_bar->add_child(col_nb);
  939. col_nb->set_valign(Label::VALIGN_CENTER);
  940. col_nb->set_v_size_flags(SIZE_FILL);
  941. col_nb->set_autowrap(true); // workaround to prevent resizing the label on each change
  942. col_nb->set_custom_minimum_size(Size2(40, 1) * EDSCALE);
  943. text_editor->connect("gui_input", this, "_text_editor_gui_input");
  944. text_editor->connect("cursor_changed", this, "_line_col_changed");
  945. text_editor->connect("text_changed", this, "_text_changed");
  946. text_editor->connect("request_completion", this, "_complete_request");
  947. Vector<String> cs;
  948. cs.push_back(".");
  949. cs.push_back(",");
  950. cs.push_back("(");
  951. cs.push_back("$");
  952. text_editor->set_completion(true, cs);
  953. idle->connect("timeout", this, "_text_changed_idle_timeout");
  954. code_complete_timer->connect("timeout", this, "_code_complete_timer_timeout");
  955. font_resize_val = 0;
  956. font_resize_timer = memnew(Timer);
  957. add_child(font_resize_timer);
  958. font_resize_timer->set_one_shot(true);
  959. font_resize_timer->set_wait_time(0.07);
  960. font_resize_timer->connect("timeout", this, "_font_resize_timeout");
  961. EditorSettings::get_singleton()->connect("settings_changed", this, "_on_settings_change");
  962. }