find_in_files.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. /**************************************************************************/
  2. /* find_in_files.h */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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/templates/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. void set_search_text(String p_pattern);
  41. void set_whole_words(bool p_whole_word);
  42. void set_match_case(bool p_match_case);
  43. void set_folder(String folder);
  44. void set_filter(const HashSet<String> &exts);
  45. String get_search_text() const { return _pattern; }
  46. bool is_whole_words() const { return _whole_words; }
  47. bool is_match_case() const { return _match_case; }
  48. void start();
  49. void stop();
  50. bool is_searching() const { return _searching; }
  51. float get_progress() const;
  52. protected:
  53. void _notification(int p_what);
  54. static void _bind_methods();
  55. private:
  56. void _process();
  57. void _iterate();
  58. void _scan_dir(String path, PackedStringArray &out_folders);
  59. void _scan_file(String fpath);
  60. // Config
  61. String _pattern;
  62. HashSet<String> _extension_filter;
  63. String _root_dir;
  64. bool _whole_words = true;
  65. bool _match_case = true;
  66. // State
  67. bool _searching = false;
  68. String _current_dir;
  69. Vector<PackedStringArray> _folders_stack;
  70. Vector<String> _files_to_scan;
  71. int _initial_files_count = 0;
  72. };
  73. class LineEdit;
  74. class CheckBox;
  75. class FileDialog;
  76. class HBoxContainer;
  77. // Prompts search parameters
  78. class FindInFilesDialog : public AcceptDialog {
  79. GDCLASS(FindInFilesDialog, AcceptDialog);
  80. public:
  81. enum FindInFilesMode {
  82. SEARCH_MODE,
  83. REPLACE_MODE
  84. };
  85. static const char *SIGNAL_FIND_REQUESTED;
  86. static const char *SIGNAL_REPLACE_REQUESTED;
  87. FindInFilesDialog();
  88. void set_search_text(String text);
  89. void set_replace_text(String text);
  90. void set_find_in_files_mode(FindInFilesMode p_mode);
  91. String get_search_text() const;
  92. String get_replace_text() const;
  93. bool is_match_case() const;
  94. bool is_whole_words() const;
  95. String get_folder() const;
  96. HashSet<String> get_filter() const;
  97. protected:
  98. void _notification(int p_what);
  99. void _visibility_changed();
  100. void custom_action(const String &p_action) override;
  101. static void _bind_methods();
  102. private:
  103. void _on_folder_button_pressed();
  104. void _on_folder_selected(String path);
  105. void _on_search_text_modified(String text);
  106. void _on_search_text_submitted(String text);
  107. void _on_replace_text_submitted(String text);
  108. FindInFilesMode _mode;
  109. LineEdit *_search_text_line_edit = nullptr;
  110. Label *_replace_label = nullptr;
  111. LineEdit *_replace_text_line_edit = nullptr;
  112. LineEdit *_folder_line_edit = nullptr;
  113. CheckBox *_match_case_checkbox = nullptr;
  114. CheckBox *_whole_words_checkbox = nullptr;
  115. Button *_find_button = nullptr;
  116. Button *_replace_button = nullptr;
  117. FileDialog *_folder_dialog = nullptr;
  118. HBoxContainer *_filters_container = nullptr;
  119. HashMap<String, bool> _filters_preferences;
  120. };
  121. class Button;
  122. class Tree;
  123. class TreeItem;
  124. class ProgressBar;
  125. // Display search results
  126. class FindInFilesPanel : public Control {
  127. GDCLASS(FindInFilesPanel, Control);
  128. public:
  129. static const char *SIGNAL_RESULT_SELECTED;
  130. static const char *SIGNAL_FILES_MODIFIED;
  131. FindInFilesPanel();
  132. FindInFiles *get_finder() const { return _finder; }
  133. void set_with_replace(bool with_replace);
  134. void set_replace_text(String text);
  135. void start_search();
  136. void stop_search();
  137. protected:
  138. static void _bind_methods();
  139. void _notification(int p_what);
  140. private:
  141. void _on_result_found(String fpath, int line_number, int begin, int end, String text);
  142. void _on_finished();
  143. void _on_refresh_button_clicked();
  144. void _on_cancel_button_clicked();
  145. void _on_result_selected();
  146. void _on_item_edited();
  147. void _on_replace_text_changed(String text);
  148. void _on_replace_all_clicked();
  149. struct Result {
  150. int line_number = 0;
  151. int begin = 0;
  152. int end = 0;
  153. int begin_trimmed = 0;
  154. };
  155. void apply_replaces_in_file(String fpath, const Vector<Result> &locations, String new_text);
  156. void update_replace_buttons();
  157. String get_replace_text();
  158. void draw_result_text(Object *item_obj, Rect2 rect);
  159. void set_progress_visible(bool p_visible);
  160. void clear();
  161. FindInFiles *_finder = nullptr;
  162. Label *_search_text_label = nullptr;
  163. Tree *_results_display = nullptr;
  164. Label *_status_label = nullptr;
  165. Button *_refresh_button = nullptr;
  166. Button *_cancel_button = nullptr;
  167. ProgressBar *_progress_bar = nullptr;
  168. HashMap<String, TreeItem *> _file_items;
  169. HashMap<TreeItem *, Result> _result_items;
  170. bool _with_replace = false;
  171. HBoxContainer *_replace_container = nullptr;
  172. LineEdit *_replace_line_edit = nullptr;
  173. Button *_replace_all_button = nullptr;
  174. };
  175. #endif // FIND_IN_FILES_H