find_in_files.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 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 "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. FindInFiles();
  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 Set<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_notification);
  54. static void _bind_methods();
  55. private:
  56. void _process();
  57. void _iterate();
  58. void _scan_dir(String path, PoolStringArray &out_folders);
  59. void _scan_file(String fpath);
  60. // Config
  61. String _pattern;
  62. Set<String> _extension_filter;
  63. String _root_dir;
  64. bool _whole_words;
  65. bool _match_case;
  66. // State
  67. bool _searching;
  68. String _current_dir;
  69. Vector<PoolStringArray> _folders_stack;
  70. Vector<String> _files_to_scan;
  71. int _initial_files_count;
  72. };
  73. class LineEdit;
  74. class CheckBox;
  75. class FileDialog;
  76. // Prompts search parameters
  77. class FindInFilesDialog : public AcceptDialog {
  78. GDCLASS(FindInFilesDialog, AcceptDialog)
  79. public:
  80. static const char *SIGNAL_FIND_REQUESTED;
  81. static const char *SIGNAL_REPLACE_REQUESTED;
  82. FindInFilesDialog();
  83. void set_search_text(String text);
  84. String get_search_text() const;
  85. bool is_match_case() const;
  86. bool is_whole_words() const;
  87. String get_folder() const;
  88. Set<String> get_filter() const;
  89. protected:
  90. static void _bind_methods();
  91. void _notification(int p_what);
  92. void custom_action(const String &p_action);
  93. private:
  94. void _on_folder_button_pressed();
  95. void _on_folder_selected(String path);
  96. void _on_search_text_modified(String text);
  97. void _on_search_text_entered(String text);
  98. LineEdit *_search_text_line_edit;
  99. LineEdit *_folder_line_edit;
  100. Vector<CheckBox *> _filters;
  101. CheckBox *_match_case_checkbox;
  102. CheckBox *_whole_words_checkbox;
  103. Button *_find_button;
  104. Button *_replace_button;
  105. FileDialog *_folder_dialog;
  106. };
  107. class Button;
  108. class Tree;
  109. class TreeItem;
  110. class ProgressBar;
  111. // Display search results
  112. class FindInFilesPanel : public Control {
  113. GDCLASS(FindInFilesPanel, Control)
  114. public:
  115. static const char *SIGNAL_RESULT_SELECTED;
  116. static const char *SIGNAL_FILES_MODIFIED;
  117. FindInFilesPanel();
  118. FindInFiles *get_finder() const { return _finder; }
  119. void set_with_replace(bool with_replace);
  120. void start_search();
  121. void stop_search();
  122. protected:
  123. static void _bind_methods();
  124. void _notification(int p_what);
  125. private:
  126. void _on_result_found(String fpath, int line_number, int begin, int end, String text);
  127. void _on_finished();
  128. void _on_cancel_button_clicked();
  129. void _on_result_selected();
  130. void _on_item_edited();
  131. void _on_replace_text_changed(String text);
  132. void _on_replace_all_clicked();
  133. struct Result {
  134. int line_number;
  135. int begin;
  136. int end;
  137. float draw_begin;
  138. float draw_width;
  139. };
  140. void apply_replaces_in_file(String fpath, const Vector<Result> &locations, String new_text);
  141. void update_replace_buttons();
  142. String get_replace_text();
  143. void draw_result_text(Object *item_obj, Rect2 rect);
  144. void set_progress_visible(bool visible);
  145. void clear();
  146. FindInFiles *_finder;
  147. Label *_search_text_label;
  148. Tree *_results_display;
  149. Label *_status_label;
  150. Button *_cancel_button;
  151. ProgressBar *_progress_bar;
  152. Map<String, TreeItem *> _file_items;
  153. Map<TreeItem *, Result> _result_items;
  154. bool _with_replace;
  155. HBoxContainer *_replace_container;
  156. LineEdit *_replace_line_edit;
  157. Button *_replace_all_button;
  158. };
  159. #endif // FIND_IN_FILES_H