find_in_files.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /*************************************************************************/
  2. /* find_in_files.h */
  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. #ifndef FIND_IN_FILES_H
  31. #define FIND_IN_FILES_H
  32. #include "core/hash_map.h"
  33. #include "scene/gui/dialogs.h"
  34. // Performs the actual search
  35. class FindInFiles : public Node {
  36. GDCLASS(FindInFiles, Node);
  37. public:
  38. static const char *SIGNAL_RESULT_FOUND;
  39. static const char *SIGNAL_FINISHED;
  40. FindInFiles();
  41. void set_search_text(String p_pattern);
  42. void set_whole_words(bool p_whole_word);
  43. void set_match_case(bool p_match_case);
  44. void set_folder(String folder);
  45. void set_filter(const Set<String> &exts);
  46. String get_search_text() const { return _pattern; }
  47. bool is_whole_words() const { return _whole_words; }
  48. bool is_match_case() const { return _match_case; }
  49. void start();
  50. void stop();
  51. bool is_searching() const { return _searching; }
  52. float get_progress() const;
  53. protected:
  54. void _notification(int p_notification);
  55. static void _bind_methods();
  56. private:
  57. void _process();
  58. void _iterate();
  59. void _scan_dir(String path, PackedStringArray &out_folders);
  60. void _scan_file(String fpath);
  61. // Config
  62. String _pattern;
  63. Set<String> _extension_filter;
  64. String _root_dir;
  65. bool _whole_words;
  66. bool _match_case;
  67. // State
  68. bool _searching;
  69. String _current_dir;
  70. Vector<PackedStringArray> _folders_stack;
  71. Vector<String> _files_to_scan;
  72. int _initial_files_count;
  73. };
  74. class LineEdit;
  75. class CheckBox;
  76. class FileDialog;
  77. class HBoxContainer;
  78. // Prompts search parameters
  79. class FindInFilesDialog : public AcceptDialog {
  80. GDCLASS(FindInFilesDialog, AcceptDialog);
  81. public:
  82. enum FindInFilesMode {
  83. SEARCH_MODE,
  84. REPLACE_MODE
  85. };
  86. static const char *SIGNAL_FIND_REQUESTED;
  87. static const char *SIGNAL_REPLACE_REQUESTED;
  88. FindInFilesDialog();
  89. void set_search_text(String text);
  90. void set_replace_text(String text);
  91. void set_find_in_files_mode(FindInFilesMode p_mode);
  92. String get_search_text() const;
  93. String get_replace_text() const;
  94. bool is_match_case() const;
  95. bool is_whole_words() const;
  96. String get_folder() const;
  97. Set<String> get_filter() const;
  98. protected:
  99. void _notification(int p_what);
  100. void _visibility_changed();
  101. void custom_action(const String &p_action);
  102. static void _bind_methods();
  103. private:
  104. void _on_folder_button_pressed();
  105. void _on_folder_selected(String path);
  106. void _on_search_text_modified(String text);
  107. void _on_search_text_entered(String text);
  108. void _on_replace_text_entered(String text);
  109. FindInFilesMode _mode;
  110. LineEdit *_search_text_line_edit;
  111. Label *_replace_label;
  112. LineEdit *_replace_text_line_edit;
  113. LineEdit *_folder_line_edit;
  114. CheckBox *_match_case_checkbox;
  115. CheckBox *_whole_words_checkbox;
  116. Button *_find_button;
  117. Button *_replace_button;
  118. FileDialog *_folder_dialog;
  119. HBoxContainer *_filters_container;
  120. HashMap<String, bool> _filters_preferences;
  121. };
  122. class Button;
  123. class Tree;
  124. class TreeItem;
  125. class ProgressBar;
  126. // Display search results
  127. class FindInFilesPanel : public Control {
  128. GDCLASS(FindInFilesPanel, Control);
  129. public:
  130. static const char *SIGNAL_RESULT_SELECTED;
  131. static const char *SIGNAL_FILES_MODIFIED;
  132. FindInFilesPanel();
  133. FindInFiles *get_finder() const { return _finder; }
  134. void set_with_replace(bool with_replace);
  135. void set_replace_text(String text);
  136. void start_search();
  137. void stop_search();
  138. protected:
  139. static void _bind_methods();
  140. void _notification(int p_what);
  141. private:
  142. void _on_result_found(String fpath, int line_number, int begin, int end, String text);
  143. void _on_finished();
  144. void _on_refresh_button_clicked();
  145. void _on_cancel_button_clicked();
  146. void _on_result_selected();
  147. void _on_item_edited();
  148. void _on_replace_text_changed(String text);
  149. void _on_replace_all_clicked();
  150. struct Result {
  151. int line_number;
  152. int begin;
  153. int end;
  154. float draw_begin;
  155. float draw_width;
  156. };
  157. void apply_replaces_in_file(String fpath, const Vector<Result> &locations, String new_text);
  158. void update_replace_buttons();
  159. String get_replace_text();
  160. void draw_result_text(Object *item_obj, Rect2 rect);
  161. void set_progress_visible(bool visible);
  162. void clear();
  163. FindInFiles *_finder;
  164. Label *_search_text_label;
  165. Tree *_results_display;
  166. Label *_status_label;
  167. Button *_refresh_button;
  168. Button *_cancel_button;
  169. ProgressBar *_progress_bar;
  170. Map<String, TreeItem *> _file_items;
  171. Map<TreeItem *, Result> _result_items;
  172. bool _with_replace;
  173. HBoxContainer *_replace_container;
  174. LineEdit *_replace_line_edit;
  175. Button *_replace_all_button;
  176. };
  177. #endif // FIND_IN_FILES_H