find_in_files.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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/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, PoolStringArray &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<PoolStringArray> _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. static void _bind_methods();
  100. void _notification(int p_what);
  101. void custom_action(const String &p_action);
  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_entered(String text);
  107. void _on_replace_text_entered(String text);
  108. FindInFilesMode _mode;
  109. LineEdit *_search_text_line_edit;
  110. Label *_replace_label;
  111. LineEdit *_replace_text_line_edit;
  112. LineEdit *_folder_line_edit;
  113. CheckBox *_match_case_checkbox;
  114. CheckBox *_whole_words_checkbox;
  115. Button *_find_button;
  116. Button *_replace_button;
  117. FileDialog *_folder_dialog;
  118. HBoxContainer *_filters_container;
  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;
  151. int begin;
  152. int end;
  153. int begin_trimmed;
  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 visible);
  160. void clear();
  161. FindInFiles *_finder;
  162. Label *_search_text_label;
  163. Tree *_results_display;
  164. Label *_status_label;
  165. Button *_refresh_button;
  166. Button *_cancel_button;
  167. ProgressBar *_progress_bar;
  168. Map<String, TreeItem *> _file_items;
  169. Map<TreeItem *, Result> _result_items;
  170. bool _with_replace;
  171. HBoxContainer *_replace_container;
  172. LineEdit *_replace_line_edit;
  173. Button *_replace_all_button;
  174. };
  175. #endif // FIND_IN_FILES_H