2
0

find_in_files.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824
  1. /*************************************************************************/
  2. /* find_in_files.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2018 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2018 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 "find_in_files.h"
  31. #include "core/os/dir_access.h"
  32. #include "core/os/os.h"
  33. #include "editor_scale.h"
  34. #include "scene/gui/box_container.h"
  35. #include "scene/gui/button.h"
  36. #include "scene/gui/check_box.h"
  37. #include "scene/gui/file_dialog.h"
  38. #include "scene/gui/grid_container.h"
  39. #include "scene/gui/item_list.h"
  40. #include "scene/gui/label.h"
  41. #include "scene/gui/line_edit.h"
  42. #include "scene/gui/progress_bar.h"
  43. #define ROOT_PREFIX "res://"
  44. const char *FindInFiles::SIGNAL_RESULT_FOUND = "result_found";
  45. const char *FindInFiles::SIGNAL_FINISHED = "finished";
  46. // TODO Would be nice in Vector and PoolVectors
  47. template <typename T>
  48. inline void pop_back(T &container) {
  49. container.resize(container.size() - 1);
  50. }
  51. // TODO Copied from TextEdit private, would be nice to extract it in a single place
  52. static bool is_text_char(CharType c) {
  53. return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') || c == '_';
  54. }
  55. FindInFiles::FindInFiles() {
  56. _root_prefix = ROOT_PREFIX;
  57. _extension_filter.insert("gd");
  58. _extension_filter.insert("cs");
  59. _searching = false;
  60. _whole_words = true;
  61. _match_case = true;
  62. }
  63. void FindInFiles::set_search_text(String p_pattern) {
  64. _pattern = p_pattern;
  65. }
  66. void FindInFiles::set_whole_words(bool p_whole_word) {
  67. _whole_words = p_whole_word;
  68. }
  69. void FindInFiles::set_match_case(bool p_match_case) {
  70. _match_case = p_match_case;
  71. }
  72. void FindInFiles::set_folder(String folder) {
  73. _root_dir = folder;
  74. }
  75. void FindInFiles::set_filter(const Set<String> &exts) {
  76. _extension_filter = exts;
  77. }
  78. void FindInFiles::_notification(int p_notification) {
  79. if (p_notification == NOTIFICATION_PROCESS) {
  80. _process();
  81. }
  82. }
  83. void FindInFiles::start() {
  84. if (_pattern == "") {
  85. print_line("Nothing to search, pattern is empty");
  86. emit_signal(SIGNAL_FINISHED);
  87. return;
  88. }
  89. if (_extension_filter.size() == 0) {
  90. print_line("Nothing to search, filter matches no files");
  91. emit_signal(SIGNAL_FINISHED);
  92. return;
  93. }
  94. // Init search
  95. _current_dir = "";
  96. PoolStringArray init_folder;
  97. init_folder.append(_root_dir);
  98. _folders_stack.push_back(init_folder);
  99. _initial_files_count = 0;
  100. _searching = true;
  101. set_process(true);
  102. }
  103. void FindInFiles::stop() {
  104. _searching = false;
  105. _current_dir = "";
  106. set_process(false);
  107. }
  108. void FindInFiles::_process() {
  109. // This part can be moved to a thread if needed
  110. OS &os = *OS::get_singleton();
  111. float duration = 0.0;
  112. while (duration < 1.0 / 120.0) {
  113. float time_before = os.get_ticks_msec();
  114. _iterate();
  115. duration += (os.get_ticks_msec() - time_before);
  116. }
  117. }
  118. void FindInFiles::_iterate() {
  119. if (_folders_stack.size() != 0) {
  120. // Scan folders first so we can build a list of files and have progress info later
  121. PoolStringArray &folders_to_scan = _folders_stack[_folders_stack.size() - 1];
  122. if (folders_to_scan.size() != 0) {
  123. // Scan one folder below
  124. String folder_name = folders_to_scan[folders_to_scan.size() - 1];
  125. pop_back(folders_to_scan);
  126. _current_dir = _current_dir.plus_file(folder_name);
  127. PoolStringArray sub_dirs;
  128. _scan_dir(_root_prefix + _current_dir, sub_dirs);
  129. _folders_stack.push_back(sub_dirs);
  130. } else {
  131. // Go back one level
  132. pop_back(_folders_stack);
  133. _current_dir = _current_dir.get_base_dir();
  134. if (_folders_stack.size() == 0) {
  135. // All folders scanned
  136. _initial_files_count = _files_to_scan.size();
  137. }
  138. }
  139. } else if (_files_to_scan.size() != 0) {
  140. // Then scan files
  141. String fpath = _files_to_scan[_files_to_scan.size() - 1];
  142. pop_back(_files_to_scan);
  143. _scan_file(fpath);
  144. } else {
  145. print_line("Search complete");
  146. set_process(false);
  147. _current_dir = "";
  148. _searching = false;
  149. emit_signal(SIGNAL_FINISHED);
  150. }
  151. }
  152. float FindInFiles::get_progress() const {
  153. if (_initial_files_count != 0) {
  154. return static_cast<float>(_initial_files_count - _files_to_scan.size()) / static_cast<float>(_initial_files_count);
  155. }
  156. return 0;
  157. }
  158. void FindInFiles::_scan_dir(String path, PoolStringArray &out_folders) {
  159. DirAccess *dir = DirAccess::open(path);
  160. if (dir == NULL) {
  161. print_line("Cannot open directory! " + path);
  162. return;
  163. }
  164. dir->list_dir_begin();
  165. for (int i = 0; i < 1000; ++i) {
  166. String file = dir->get_next();
  167. if (file == "")
  168. break;
  169. // Ignore special dirs and hidden dirs (such as .git and .import)
  170. if (file == "." || file == ".." || file.begins_with("."))
  171. continue;
  172. if (dir->current_is_dir())
  173. out_folders.append(file);
  174. else {
  175. String file_ext = file.get_extension();
  176. if (_extension_filter.has(file_ext)) {
  177. _files_to_scan.push_back(path.plus_file(file));
  178. }
  179. }
  180. }
  181. }
  182. void FindInFiles::_scan_file(String fpath) {
  183. FileAccess *f = FileAccess::open(fpath, FileAccess::READ);
  184. if (f == NULL) {
  185. print_line(String("Cannot open file ") + fpath);
  186. return;
  187. }
  188. int line_number = 0;
  189. while (!f->eof_reached()) {
  190. // line number starts at 1
  191. ++line_number;
  192. int begin = 0;
  193. int end = 0;
  194. String line = f->get_line();
  195. // Find all occurrences in the current line
  196. while (true) {
  197. begin = _match_case ? line.find(_pattern, end) : line.findn(_pattern, end);
  198. if (begin == -1)
  199. break;
  200. end = begin + _pattern.length();
  201. if (_whole_words) {
  202. if (begin > 0 && is_text_char(line[begin - 1])) {
  203. continue;
  204. }
  205. if (end < line.size() && is_text_char(line[end])) {
  206. continue;
  207. }
  208. }
  209. emit_signal(SIGNAL_RESULT_FOUND, fpath, line_number, begin, end, line);
  210. }
  211. }
  212. f->close();
  213. }
  214. void FindInFiles::_bind_methods() {
  215. ADD_SIGNAL(MethodInfo(SIGNAL_RESULT_FOUND,
  216. PropertyInfo(Variant::STRING, "path"),
  217. PropertyInfo(Variant::INT, "line_number"),
  218. PropertyInfo(Variant::INT, "begin"),
  219. PropertyInfo(Variant::INT, "end"),
  220. PropertyInfo(Variant::STRING, "text")));
  221. ADD_SIGNAL(MethodInfo(SIGNAL_FINISHED));
  222. }
  223. //-----------------------------------------------------------------------------
  224. const char *FindInFilesDialog::SIGNAL_FIND_REQUESTED = "find_requested";
  225. const char *FindInFilesDialog::SIGNAL_REPLACE_REQUESTED = "replace_requested";
  226. FindInFilesDialog::FindInFilesDialog() {
  227. set_custom_minimum_size(Size2(400, 190));
  228. set_resizable(true);
  229. set_title(TTR("Find in files"));
  230. VBoxContainer *vbc = memnew(VBoxContainer);
  231. vbc->set_anchor_and_margin(MARGIN_LEFT, ANCHOR_BEGIN, 8 * EDSCALE);
  232. vbc->set_anchor_and_margin(MARGIN_TOP, ANCHOR_BEGIN, 8 * EDSCALE);
  233. vbc->set_anchor_and_margin(MARGIN_RIGHT, ANCHOR_END, -8 * EDSCALE);
  234. vbc->set_anchor_and_margin(MARGIN_BOTTOM, ANCHOR_END, -8 * EDSCALE);
  235. add_child(vbc);
  236. GridContainer *gc = memnew(GridContainer);
  237. gc->set_columns(2);
  238. vbc->add_child(gc);
  239. Label *find_label = memnew(Label);
  240. find_label->set_text(TTR("Find: "));
  241. gc->add_child(find_label);
  242. _search_text_line_edit = memnew(LineEdit);
  243. _search_text_line_edit->set_h_size_flags(SIZE_EXPAND_FILL);
  244. _search_text_line_edit->connect("text_changed", this, "_on_search_text_modified");
  245. _search_text_line_edit->connect("text_entered", this, "_on_search_text_entered");
  246. gc->add_child(_search_text_line_edit);
  247. {
  248. Control *placeholder = memnew(Control);
  249. gc->add_child(placeholder);
  250. }
  251. {
  252. HBoxContainer *hbc = memnew(HBoxContainer);
  253. _whole_words_checkbox = memnew(CheckBox);
  254. _whole_words_checkbox->set_text(TTR("Whole words"));
  255. _whole_words_checkbox->set_pressed(true);
  256. hbc->add_child(_whole_words_checkbox);
  257. _match_case_checkbox = memnew(CheckBox);
  258. _match_case_checkbox->set_text(TTR("Match case"));
  259. _match_case_checkbox->set_pressed(true);
  260. hbc->add_child(_match_case_checkbox);
  261. gc->add_child(hbc);
  262. }
  263. Label *folder_label = memnew(Label);
  264. folder_label->set_text(TTR("Folder: "));
  265. gc->add_child(folder_label);
  266. {
  267. HBoxContainer *hbc = memnew(HBoxContainer);
  268. Label *prefix_label = memnew(Label);
  269. prefix_label->set_text(ROOT_PREFIX);
  270. hbc->add_child(prefix_label);
  271. _folder_line_edit = memnew(LineEdit);
  272. _folder_line_edit->set_h_size_flags(SIZE_EXPAND_FILL);
  273. hbc->add_child(_folder_line_edit);
  274. Button *folder_button = memnew(Button);
  275. folder_button->set_text("...");
  276. folder_button->connect("pressed", this, "_on_folder_button_pressed");
  277. hbc->add_child(folder_button);
  278. _folder_dialog = memnew(FileDialog);
  279. _folder_dialog->set_mode(FileDialog::MODE_OPEN_DIR);
  280. _folder_dialog->connect("dir_selected", this, "_on_folder_selected");
  281. add_child(_folder_dialog);
  282. gc->add_child(hbc);
  283. }
  284. Label *filter_label = memnew(Label);
  285. filter_label->set_text(TTR("Filter: "));
  286. gc->add_child(filter_label);
  287. {
  288. HBoxContainer *hbc = memnew(HBoxContainer);
  289. Vector<String> exts;
  290. exts.push_back("gd");
  291. exts.push_back("cs");
  292. for (int i = 0; i < exts.size(); ++i) {
  293. CheckBox *cb = memnew(CheckBox);
  294. cb->set_text(exts[i]);
  295. cb->set_pressed(true);
  296. hbc->add_child(cb);
  297. _filters.push_back(cb);
  298. }
  299. gc->add_child(hbc);
  300. }
  301. {
  302. Control *placeholder = memnew(Control);
  303. placeholder->set_custom_minimum_size(Size2(0, EDSCALE * 16));
  304. vbc->add_child(placeholder);
  305. }
  306. {
  307. HBoxContainer *hbc = memnew(HBoxContainer);
  308. hbc->set_alignment(HBoxContainer::ALIGN_CENTER);
  309. _find_button = memnew(Button);
  310. _find_button->set_text(TTR("Find..."));
  311. _find_button->connect("pressed", this, "_on_find_button_pressed");
  312. _find_button->set_disabled(true);
  313. hbc->add_child(_find_button);
  314. {
  315. Control *placeholder = memnew(Control);
  316. placeholder->set_custom_minimum_size(Size2(EDSCALE * 16, 0));
  317. hbc->add_child(placeholder);
  318. }
  319. _replace_button = memnew(Button);
  320. _replace_button->set_text(TTR("Replace..."));
  321. _replace_button->connect("pressed", this, "_on_replace_button_pressed");
  322. _replace_button->set_disabled(true);
  323. hbc->add_child(_replace_button);
  324. {
  325. Control *placeholder = memnew(Control);
  326. placeholder->set_custom_minimum_size(Size2(EDSCALE * 16, 0));
  327. hbc->add_child(placeholder);
  328. }
  329. Button *cancel_button = memnew(Button);
  330. cancel_button->set_text(TTR("Cancel"));
  331. cancel_button->connect("pressed", this, "hide");
  332. hbc->add_child(cancel_button);
  333. vbc->add_child(hbc);
  334. }
  335. }
  336. void FindInFilesDialog::set_search_text(String text) {
  337. _search_text_line_edit->set_text(text);
  338. }
  339. String FindInFilesDialog::get_search_text() const {
  340. String text = _search_text_line_edit->get_text();
  341. return text.strip_edges();
  342. }
  343. bool FindInFilesDialog::is_match_case() const {
  344. return _match_case_checkbox->is_pressed();
  345. }
  346. bool FindInFilesDialog::is_whole_words() const {
  347. return _whole_words_checkbox->is_pressed();
  348. }
  349. String FindInFilesDialog::get_folder() const {
  350. String text = _folder_line_edit->get_text();
  351. return text.strip_edges();
  352. }
  353. Set<String> FindInFilesDialog::get_filter() const {
  354. Set<String> filters;
  355. for (int i = 0; i < _filters.size(); ++i) {
  356. CheckBox *cb = _filters[i];
  357. if (cb->is_pressed()) {
  358. filters.insert(_filters[i]->get_text());
  359. }
  360. }
  361. return filters;
  362. }
  363. void FindInFilesDialog::_notification(int p_what) {
  364. if (p_what == NOTIFICATION_VISIBILITY_CHANGED) {
  365. if (is_visible()) {
  366. // Doesn't work more than once if not deferred...
  367. _search_text_line_edit->call_deferred("grab_focus");
  368. _search_text_line_edit->select_all();
  369. }
  370. }
  371. }
  372. void FindInFilesDialog::_on_folder_button_pressed() {
  373. _folder_dialog->popup_centered_ratio();
  374. }
  375. void FindInFilesDialog::_on_find_button_pressed() {
  376. emit_signal(SIGNAL_FIND_REQUESTED);
  377. hide();
  378. }
  379. void FindInFilesDialog::_on_replace_button_pressed() {
  380. emit_signal(SIGNAL_REPLACE_REQUESTED);
  381. hide();
  382. }
  383. void FindInFilesDialog::_on_search_text_modified(String text) {
  384. ERR_FAIL_COND(!_find_button);
  385. ERR_FAIL_COND(!_replace_button);
  386. _find_button->set_disabled(get_search_text().empty());
  387. _replace_button->set_disabled(get_search_text().empty());
  388. }
  389. void FindInFilesDialog::_on_search_text_entered(String text) {
  390. // This allows to trigger a global search without leaving the keyboard
  391. if (!_find_button->is_disabled())
  392. _on_find_button_pressed();
  393. }
  394. void FindInFilesDialog::_on_folder_selected(String path) {
  395. int i = path.find("://");
  396. if (i != -1)
  397. path = path.right(i + 3);
  398. _folder_line_edit->set_text(path);
  399. }
  400. void FindInFilesDialog::_bind_methods() {
  401. ClassDB::bind_method("_on_folder_button_pressed", &FindInFilesDialog::_on_folder_button_pressed);
  402. ClassDB::bind_method("_on_find_button_pressed", &FindInFilesDialog::_on_find_button_pressed);
  403. ClassDB::bind_method("_on_replace_button_pressed", &FindInFilesDialog::_on_replace_button_pressed);
  404. ClassDB::bind_method("_on_folder_selected", &FindInFilesDialog::_on_folder_selected);
  405. ClassDB::bind_method("_on_search_text_modified", &FindInFilesDialog::_on_search_text_modified);
  406. ClassDB::bind_method("_on_search_text_entered", &FindInFilesDialog::_on_search_text_entered);
  407. ADD_SIGNAL(MethodInfo(SIGNAL_FIND_REQUESTED));
  408. ADD_SIGNAL(MethodInfo(SIGNAL_REPLACE_REQUESTED));
  409. }
  410. //-----------------------------------------------------------------------------
  411. const char *FindInFilesPanel::SIGNAL_RESULT_SELECTED = "result_selected";
  412. const char *FindInFilesPanel::SIGNAL_FILES_MODIFIED = "files_modified";
  413. FindInFilesPanel::FindInFilesPanel() {
  414. _finder = memnew(FindInFiles);
  415. _finder->connect(FindInFiles::SIGNAL_RESULT_FOUND, this, "_on_result_found");
  416. _finder->connect(FindInFiles::SIGNAL_FINISHED, this, "_on_finished");
  417. add_child(_finder);
  418. VBoxContainer *vbc = memnew(VBoxContainer);
  419. vbc->set_anchor_and_margin(MARGIN_LEFT, ANCHOR_BEGIN, 0);
  420. vbc->set_anchor_and_margin(MARGIN_TOP, ANCHOR_BEGIN, 0);
  421. vbc->set_anchor_and_margin(MARGIN_RIGHT, ANCHOR_END, 0);
  422. vbc->set_anchor_and_margin(MARGIN_BOTTOM, ANCHOR_END, 0);
  423. add_child(vbc);
  424. {
  425. HBoxContainer *hbc = memnew(HBoxContainer);
  426. Label *find_label = memnew(Label);
  427. find_label->set_text(TTR("Find: "));
  428. hbc->add_child(find_label);
  429. _search_text_label = memnew(Label);
  430. _search_text_label->add_font_override("font", get_font("source", "EditorFonts"));
  431. hbc->add_child(_search_text_label);
  432. _progress_bar = memnew(ProgressBar);
  433. _progress_bar->set_h_size_flags(SIZE_EXPAND_FILL);
  434. hbc->add_child(_progress_bar);
  435. set_progress_visible(false);
  436. _status_label = memnew(Label);
  437. hbc->add_child(_status_label);
  438. _cancel_button = memnew(Button);
  439. _cancel_button->set_text(TTR("Cancel"));
  440. _cancel_button->connect("pressed", this, "_on_cancel_button_clicked");
  441. _cancel_button->set_disabled(true);
  442. hbc->add_child(_cancel_button);
  443. vbc->add_child(hbc);
  444. }
  445. // In the future, this should be replaced by a more specific list container,
  446. // which can highlight text regions and change opacity for enabled/disabled states
  447. _results_display = memnew(ItemList);
  448. _results_display->add_font_override("font", get_font("source", "EditorFonts"));
  449. _results_display->set_v_size_flags(SIZE_EXPAND_FILL);
  450. _results_display->connect("item_selected", this, "_on_result_selected");
  451. vbc->add_child(_results_display);
  452. {
  453. _replace_container = memnew(HBoxContainer);
  454. Label *replace_label = memnew(Label);
  455. replace_label->set_text(TTR("Replace: "));
  456. _replace_container->add_child(replace_label);
  457. _replace_line_edit = memnew(LineEdit);
  458. _replace_line_edit->set_h_size_flags(SIZE_EXPAND_FILL);
  459. _replace_line_edit->connect("text_changed", this, "_on_replace_text_changed");
  460. _replace_container->add_child(_replace_line_edit);
  461. _replace_all_button = memnew(Button);
  462. _replace_all_button->set_text(TTR("Replace all (no undo)"));
  463. _replace_all_button->connect("pressed", this, "_on_replace_all_clicked");
  464. _replace_container->add_child(_replace_all_button);
  465. _replace_container->hide();
  466. vbc->add_child(_replace_container);
  467. }
  468. }
  469. void FindInFilesPanel::set_with_replace(bool with_replace) {
  470. _replace_container->set_visible(with_replace);
  471. }
  472. void FindInFilesPanel::start_search() {
  473. _results_display->clear();
  474. _status_label->set_text(TTR("Searching..."));
  475. _search_text_label->set_text(_finder->get_search_text());
  476. set_process(true);
  477. set_progress_visible(true);
  478. _finder->start();
  479. update_replace_buttons();
  480. _cancel_button->set_disabled(false);
  481. }
  482. void FindInFilesPanel::stop_search() {
  483. _finder->stop();
  484. _status_label->set_text("");
  485. update_replace_buttons();
  486. set_progress_visible(false);
  487. _cancel_button->set_disabled(true);
  488. }
  489. void FindInFilesPanel::_notification(int p_what) {
  490. if (p_what == NOTIFICATION_PROCESS) {
  491. _progress_bar->set_as_ratio(_finder->get_progress());
  492. }
  493. }
  494. void FindInFilesPanel::_on_result_found(String fpath, int line_number, int begin, int end, String text) {
  495. int i = _results_display->get_item_count();
  496. _results_display->add_item(fpath + ": " + String::num(line_number) + ": " + text.replace("\t", " "));
  497. _results_display->set_item_metadata(i, varray(fpath, line_number, begin, end));
  498. }
  499. void FindInFilesPanel::_on_finished() {
  500. _status_label->set_text(TTR("Search complete"));
  501. update_replace_buttons();
  502. set_progress_visible(false);
  503. _cancel_button->set_disabled(true);
  504. }
  505. void FindInFilesPanel::_on_cancel_button_clicked() {
  506. stop_search();
  507. }
  508. void FindInFilesPanel::_on_result_selected(int i) {
  509. Array meta = _results_display->get_item_metadata(i);
  510. emit_signal(SIGNAL_RESULT_SELECTED, meta[0], meta[1], meta[2], meta[3]);
  511. }
  512. void FindInFilesPanel::_on_replace_text_changed(String text) {
  513. update_replace_buttons();
  514. }
  515. void FindInFilesPanel::_on_replace_all_clicked() {
  516. String replace_text = get_replace_text();
  517. ERR_FAIL_COND(replace_text.empty());
  518. String last_fpath;
  519. PoolIntArray locations;
  520. PoolStringArray modified_files;
  521. for (int i = 0; i < _results_display->get_item_count(); ++i) {
  522. Array meta = _results_display->get_item_metadata(i);
  523. String fpath = meta[0];
  524. // Results are sorted by file, so we can batch replaces
  525. if (fpath != last_fpath) {
  526. if (locations.size() != 0) {
  527. apply_replaces_in_file(last_fpath, locations, replace_text);
  528. modified_files.append(last_fpath);
  529. locations.resize(0);
  530. }
  531. }
  532. locations.append(meta[1]); // line_number
  533. locations.append(meta[2]); // begin
  534. locations.append(meta[3]); // end
  535. last_fpath = fpath;
  536. }
  537. if (locations.size() != 0) {
  538. apply_replaces_in_file(last_fpath, locations, replace_text);
  539. modified_files.append(last_fpath);
  540. }
  541. // Hide replace bar so we can't trigger the action twice without doing a new search
  542. set_with_replace(false);
  543. emit_signal(SIGNAL_FILES_MODIFIED, modified_files);
  544. }
  545. // Same as get_line, but preserves line ending characters
  546. class ConservativeGetLine {
  547. public:
  548. String get_line(FileAccess *f) {
  549. _line_buffer.clear();
  550. CharType c = f->get_8();
  551. while (!f->eof_reached()) {
  552. if (c == '\n') {
  553. _line_buffer.push_back(c);
  554. _line_buffer.push_back(0);
  555. return String::utf8(_line_buffer.ptr());
  556. } else if (c == '\0') {
  557. _line_buffer.push_back(c);
  558. return String::utf8(_line_buffer.ptr());
  559. } else if (c != '\r') {
  560. _line_buffer.push_back(c);
  561. }
  562. c = f->get_8();
  563. }
  564. _line_buffer.push_back(0);
  565. return String::utf8(_line_buffer.ptr());
  566. }
  567. private:
  568. Vector<char> _line_buffer;
  569. };
  570. void FindInFilesPanel::apply_replaces_in_file(String fpath, PoolIntArray locations, String text) {
  571. ERR_FAIL_COND(locations.size() % 3 != 0);
  572. //print_line(String("Replacing {0} occurrences in {1}").format(varray(fpath, locations.size() / 3)));
  573. // If the file is already open, I assume the editor will reload it.
  574. // If there are unsaved changes, the user will be asked on focus,
  575. // however that means either loosing changes or loosing replaces.
  576. FileAccess *f = FileAccess::open(fpath, FileAccess::READ);
  577. ERR_FAIL_COND(f == NULL);
  578. String buffer;
  579. int current_line = 1;
  580. ConservativeGetLine conservative;
  581. String line = conservative.get_line(f);
  582. PoolIntArray::Read locations_read = locations.read();
  583. for (int i = 0; i < locations.size(); i += 3) {
  584. int repl_line_number = locations_read[i];
  585. int repl_begin = locations_read[i + 1];
  586. int repl_end = locations_read[i + 2];
  587. while (current_line < repl_line_number) {
  588. buffer += line;
  589. line = conservative.get_line(f);
  590. ++current_line;
  591. }
  592. line = line.left(repl_begin) + text + line.right(repl_end);
  593. }
  594. buffer += line;
  595. while (!f->eof_reached()) {
  596. buffer += conservative.get_line(f);
  597. }
  598. // Now the modified contents are in the buffer, rewrite the file with our changes
  599. Error err = f->reopen(fpath, FileAccess::WRITE);
  600. ERR_FAIL_COND(err != OK);
  601. f->store_string(buffer);
  602. f->close();
  603. }
  604. String FindInFilesPanel::get_replace_text() {
  605. return _replace_line_edit->get_text().strip_edges();
  606. }
  607. void FindInFilesPanel::update_replace_buttons() {
  608. String text = get_replace_text();
  609. bool disabled = text.empty() || _finder->is_searching();
  610. _replace_all_button->set_disabled(disabled);
  611. }
  612. void FindInFilesPanel::set_progress_visible(bool visible) {
  613. _progress_bar->set_self_modulate(Color(1, 1, 1, visible ? 1 : 0));
  614. }
  615. void FindInFilesPanel::_bind_methods() {
  616. ClassDB::bind_method("_on_result_found", &FindInFilesPanel::_on_result_found);
  617. ClassDB::bind_method("_on_finished", &FindInFilesPanel::_on_finished);
  618. ClassDB::bind_method("_on_cancel_button_clicked", &FindInFilesPanel::_on_cancel_button_clicked);
  619. ClassDB::bind_method("_on_result_selected", &FindInFilesPanel::_on_result_selected);
  620. ClassDB::bind_method("_on_replace_text_changed", &FindInFilesPanel::_on_replace_text_changed);
  621. ClassDB::bind_method("_on_replace_all_clicked", &FindInFilesPanel::_on_replace_all_clicked);
  622. ADD_SIGNAL(MethodInfo(SIGNAL_RESULT_SELECTED,
  623. PropertyInfo(Variant::STRING, "path"),
  624. PropertyInfo(Variant::INT, "line_number"),
  625. PropertyInfo(Variant::INT, "begin"),
  626. PropertyInfo(Variant::INT, "end")));
  627. ADD_SIGNAL(MethodInfo(SIGNAL_FILES_MODIFIED, PropertyInfo(Variant::STRING, "paths")));
  628. }