find_in_files.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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. #pragma once
  31. #include "core/templates/hash_map.h"
  32. #include "scene/gui/dialogs.h"
  33. // Performs the actual search
  34. class FindInFiles : public Node {
  35. GDCLASS(FindInFiles, Node);
  36. public:
  37. static const char *SIGNAL_RESULT_FOUND;
  38. static const char *SIGNAL_FINISHED;
  39. void set_search_text(const String &p_pattern);
  40. void set_whole_words(bool p_whole_word);
  41. void set_match_case(bool p_match_case);
  42. void set_folder(const String &folder);
  43. void set_filter(const HashSet<String> &exts);
  44. void set_includes(const HashSet<String> &p_include_wildcards);
  45. void set_excludes(const HashSet<String> &p_exclude_wildcards);
  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_what);
  55. static void _bind_methods();
  56. private:
  57. void _process();
  58. void _iterate();
  59. void _scan_dir(const String &path, PackedStringArray &out_folders, PackedStringArray &out_files_to_scan);
  60. void _scan_file(const String &fpath);
  61. bool _is_file_matched(const HashSet<String> &p_wildcards, const String &p_file_path, bool p_case_sensitive) const;
  62. // Config
  63. String _pattern;
  64. HashSet<String> _extension_filter;
  65. HashSet<String> _include_wildcards;
  66. HashSet<String> _exclude_wildcards;
  67. String _root_dir;
  68. bool _whole_words = true;
  69. bool _match_case = true;
  70. // State
  71. bool _searching = false;
  72. String _current_dir;
  73. Vector<PackedStringArray> _folders_stack;
  74. Vector<String> _files_to_scan;
  75. int _initial_files_count = 0;
  76. };
  77. class LineEdit;
  78. class CheckBox;
  79. class FileDialog;
  80. class HBoxContainer;
  81. // Prompts search parameters
  82. class FindInFilesDialog : public AcceptDialog {
  83. GDCLASS(FindInFilesDialog, AcceptDialog);
  84. public:
  85. enum FindInFilesMode {
  86. SEARCH_MODE,
  87. REPLACE_MODE
  88. };
  89. static const char *SIGNAL_FIND_REQUESTED;
  90. static const char *SIGNAL_REPLACE_REQUESTED;
  91. FindInFilesDialog();
  92. void set_search_text(const String &text);
  93. void set_replace_text(const String &text);
  94. void set_find_in_files_mode(FindInFilesMode p_mode);
  95. String get_search_text() const;
  96. String get_replace_text() const;
  97. bool is_match_case() const;
  98. bool is_whole_words() const;
  99. String get_folder() const;
  100. HashSet<String> get_filter() const;
  101. HashSet<String> get_includes() const;
  102. HashSet<String> get_excludes() const;
  103. protected:
  104. void _notification(int p_what);
  105. void _visibility_changed();
  106. void custom_action(const String &p_action) override;
  107. static void _bind_methods();
  108. private:
  109. void _on_folder_button_pressed();
  110. void _on_folder_selected(String path);
  111. void _on_search_text_modified(const String &text);
  112. void _on_search_text_submitted(const String &text);
  113. void _on_replace_text_submitted(const String &text);
  114. String validate_filter_wildcard(const String &p_expression) const;
  115. FindInFilesMode _mode;
  116. LineEdit *_search_text_line_edit = nullptr;
  117. Label *_replace_label = nullptr;
  118. LineEdit *_replace_text_line_edit = nullptr;
  119. LineEdit *_folder_line_edit = nullptr;
  120. CheckBox *_match_case_checkbox = nullptr;
  121. CheckBox *_whole_words_checkbox = nullptr;
  122. Button *_find_button = nullptr;
  123. Button *_replace_button = nullptr;
  124. FileDialog *_folder_dialog = nullptr;
  125. HBoxContainer *_filters_container = nullptr;
  126. LineEdit *_includes_line_edit = nullptr;
  127. LineEdit *_excludes_line_edit = nullptr;
  128. HashMap<String, bool> _filters_preferences;
  129. };
  130. class Button;
  131. class Tree;
  132. class TreeItem;
  133. class ProgressBar;
  134. // Display search results
  135. class FindInFilesPanel : public Control {
  136. GDCLASS(FindInFilesPanel, Control);
  137. public:
  138. static const char *SIGNAL_RESULT_SELECTED;
  139. static const char *SIGNAL_FILES_MODIFIED;
  140. static const char *SIGNAL_CLOSE_BUTTON_CLICKED;
  141. FindInFilesPanel();
  142. FindInFiles *get_finder() const { return _finder; }
  143. void set_with_replace(bool with_replace);
  144. void set_replace_text(const String &text);
  145. void start_search();
  146. void stop_search();
  147. protected:
  148. static void _bind_methods();
  149. void _notification(int p_what);
  150. private:
  151. void _on_button_clicked(TreeItem *p_item, int p_column, int p_id, int p_mouse_button_index);
  152. void _on_result_found(const String &fpath, int line_number, int begin, int end, String text);
  153. void _on_finished();
  154. void _on_refresh_button_clicked();
  155. void _on_cancel_button_clicked();
  156. void _on_close_button_clicked();
  157. void _on_result_selected();
  158. void _on_item_edited();
  159. void _on_replace_text_changed(const String &text);
  160. void _on_replace_all_clicked();
  161. struct Result {
  162. int line_number = 0;
  163. int begin = 0;
  164. int end = 0;
  165. int begin_trimmed = 0;
  166. };
  167. void apply_replaces_in_file(const String &fpath, const Vector<Result> &locations, const String &new_text);
  168. void update_replace_buttons();
  169. void update_matches_text();
  170. String get_replace_text();
  171. void draw_result_text(Object *item_obj, Rect2 rect);
  172. void set_progress_visible(bool p_visible);
  173. void clear();
  174. FindInFiles *_finder = nullptr;
  175. Label *_search_text_label = nullptr;
  176. Tree *_results_display = nullptr;
  177. Label *_status_label = nullptr;
  178. Button *_refresh_button = nullptr;
  179. Button *_cancel_button = nullptr;
  180. Button *_close_button = nullptr;
  181. ProgressBar *_progress_bar = nullptr;
  182. HashMap<String, TreeItem *> _file_items;
  183. HashMap<TreeItem *, Result> _result_items;
  184. bool _with_replace = false;
  185. HBoxContainer *_replace_container = nullptr;
  186. LineEdit *_replace_line_edit = nullptr;
  187. Button *_replace_all_button = nullptr;
  188. };