find_in_files.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 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, 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. static const char *SIGNAL_FIND_REQUESTED;
  83. static const char *SIGNAL_REPLACE_REQUESTED;
  84. FindInFilesDialog();
  85. void set_search_text(String text);
  86. String get_search_text() const;
  87. bool is_match_case() const;
  88. bool is_whole_words() const;
  89. String get_folder() const;
  90. Set<String> get_filter() const;
  91. protected:
  92. static void _bind_methods();
  93. void _notification(int p_what);
  94. void custom_action(const String &p_action);
  95. private:
  96. void _on_folder_button_pressed();
  97. void _on_folder_selected(String path);
  98. void _on_search_text_modified(String text);
  99. void _on_search_text_entered(String text);
  100. LineEdit *_search_text_line_edit;
  101. LineEdit *_folder_line_edit;
  102. CheckBox *_match_case_checkbox;
  103. CheckBox *_whole_words_checkbox;
  104. Button *_find_button;
  105. Button *_replace_button;
  106. FileDialog *_folder_dialog;
  107. HBoxContainer *_filters_container;
  108. HashMap<String, bool> _filters_preferences;
  109. };
  110. class Button;
  111. class Tree;
  112. class TreeItem;
  113. class ProgressBar;
  114. // Display search results
  115. class FindInFilesPanel : public Control {
  116. GDCLASS(FindInFilesPanel, Control);
  117. public:
  118. static const char *SIGNAL_RESULT_SELECTED;
  119. static const char *SIGNAL_FILES_MODIFIED;
  120. FindInFilesPanel();
  121. FindInFiles *get_finder() const { return _finder; }
  122. void set_with_replace(bool with_replace);
  123. void start_search();
  124. void stop_search();
  125. protected:
  126. static void _bind_methods();
  127. void _notification(int p_what);
  128. private:
  129. void _on_result_found(String fpath, int line_number, int begin, int end, String text);
  130. void _on_finished();
  131. void _on_refresh_button_clicked();
  132. void _on_cancel_button_clicked();
  133. void _on_result_selected();
  134. void _on_item_edited();
  135. void _on_replace_text_changed(String text);
  136. void _on_replace_all_clicked();
  137. struct Result {
  138. int line_number;
  139. int begin;
  140. int end;
  141. int begin_trimmed;
  142. };
  143. void apply_replaces_in_file(String fpath, const Vector<Result> &locations, String new_text);
  144. void update_replace_buttons();
  145. String get_replace_text();
  146. void draw_result_text(Object *item_obj, Rect2 rect);
  147. void set_progress_visible(bool visible);
  148. void clear();
  149. FindInFiles *_finder;
  150. Label *_search_text_label;
  151. Tree *_results_display;
  152. Label *_status_label;
  153. Button *_refresh_button;
  154. Button *_cancel_button;
  155. ProgressBar *_progress_bar;
  156. Map<String, TreeItem *> _file_items;
  157. Map<TreeItem *, Result> _result_items;
  158. bool _with_replace;
  159. HBoxContainer *_replace_container;
  160. LineEdit *_replace_line_edit;
  161. Button *_replace_all_button;
  162. };
  163. #endif // FIND_IN_FILES_H