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