Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/editor/script/find_in_files.h
21212 views
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
#pragma once
32
33
#include "core/templates/hash_map.h"
34
#include "editor/docks/editor_dock.h"
35
#include "scene/gui/dialogs.h"
36
37
// Performs the actual search
38
class FindInFiles : public Node {
39
GDCLASS(FindInFiles, Node);
40
41
public:
42
static const char *SIGNAL_RESULT_FOUND;
43
static const char *SIGNAL_FINISHED;
44
45
void set_search_text(const 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(const String &folder);
49
void set_filter(const HashSet<String> &exts);
50
void set_includes(const HashSet<String> &p_include_wildcards);
51
void set_excludes(const HashSet<String> &p_exclude_wildcards);
52
53
String get_search_text() const { return _pattern; }
54
55
bool is_whole_words() const { return _whole_words; }
56
bool is_match_case() const { return _match_case; }
57
58
void start();
59
void stop();
60
61
bool is_searching() const { return _searching; }
62
float get_progress() const;
63
64
protected:
65
void _notification(int p_what);
66
67
static void _bind_methods();
68
69
private:
70
void _process();
71
void _iterate();
72
void _scan_dir(const String &path, PackedStringArray &out_folders, PackedStringArray &out_files_to_scan);
73
void _scan_file(const String &fpath);
74
75
bool _is_file_matched(const HashSet<String> &p_wildcards, const String &p_file_path, bool p_case_sensitive) const;
76
77
// Config
78
String _pattern;
79
HashSet<String> _extension_filter;
80
HashSet<String> _include_wildcards;
81
HashSet<String> _exclude_wildcards;
82
String _root_dir;
83
bool _whole_words = true;
84
bool _match_case = true;
85
86
// State
87
bool _searching = false;
88
String _current_dir;
89
Vector<PackedStringArray> _folders_stack;
90
Vector<String> _files_to_scan;
91
int _initial_files_count = 0;
92
};
93
94
class LineEdit;
95
class CheckBox;
96
class FileDialog;
97
class HBoxContainer;
98
99
// Prompts search parameters
100
class FindInFilesDialog : public AcceptDialog {
101
GDCLASS(FindInFilesDialog, AcceptDialog);
102
103
public:
104
enum FindInFilesMode {
105
SEARCH_MODE,
106
REPLACE_MODE
107
};
108
109
static const char *SIGNAL_FIND_REQUESTED;
110
static const char *SIGNAL_REPLACE_REQUESTED;
111
112
FindInFilesDialog();
113
114
void set_search_text(const String &text);
115
void set_replace_text(const String &text);
116
117
void set_find_in_files_mode(FindInFilesMode p_mode);
118
119
String get_search_text() const;
120
String get_replace_text() const;
121
bool is_match_case() const;
122
bool is_whole_words() const;
123
String get_folder() const;
124
HashSet<String> get_filter() const;
125
HashSet<String> get_includes() const;
126
HashSet<String> get_excludes() const;
127
128
protected:
129
void _notification(int p_what);
130
131
void _visibility_changed();
132
void custom_action(const String &p_action) override;
133
static void _bind_methods();
134
135
private:
136
void _on_folder_button_pressed();
137
void _on_folder_selected(String path);
138
void _on_search_text_modified(const String &text);
139
void _on_search_text_submitted(const String &text);
140
void _on_replace_text_submitted(const String &text);
141
142
String validate_filter_wildcard(const String &p_expression) const;
143
144
FindInFilesMode _mode;
145
LineEdit *_search_text_line_edit = nullptr;
146
147
Label *_replace_label = nullptr;
148
LineEdit *_replace_text_line_edit = nullptr;
149
150
LineEdit *_folder_line_edit = nullptr;
151
CheckBox *_match_case_checkbox = nullptr;
152
CheckBox *_whole_words_checkbox = nullptr;
153
Button *_find_button = nullptr;
154
Button *_replace_button = nullptr;
155
FileDialog *_folder_dialog = nullptr;
156
HBoxContainer *_filters_container = nullptr;
157
LineEdit *_includes_line_edit = nullptr;
158
LineEdit *_excludes_line_edit = nullptr;
159
160
HashMap<String, bool> _filters_preferences;
161
};
162
163
class Button;
164
class CheckButton;
165
class Tree;
166
class TreeItem;
167
class ProgressBar;
168
169
// Display search results
170
class FindInFilesPanel : public MarginContainer {
171
GDCLASS(FindInFilesPanel, MarginContainer);
172
173
public:
174
static const char *SIGNAL_RESULT_SELECTED;
175
static const char *SIGNAL_FILES_MODIFIED;
176
static const char *SIGNAL_CLOSE_BUTTON_CLICKED;
177
178
FindInFilesPanel();
179
180
FindInFiles *get_finder() const { return _finder; }
181
182
void set_with_replace(bool with_replace);
183
void set_replace_text(const String &text);
184
bool is_keep_results() const;
185
void set_search_labels_visibility(bool p_visible);
186
187
void start_search();
188
void stop_search();
189
190
void update_layout(EditorDock::DockLayout p_layout);
191
192
protected:
193
static void _bind_methods();
194
195
void _notification(int p_what);
196
197
private:
198
void _on_button_clicked(TreeItem *p_item, int p_column, int p_id, int p_mouse_button_index);
199
void _on_result_found(const String &fpath, int line_number, int begin, int end, String text);
200
void _on_theme_changed();
201
void _on_finished();
202
void _on_refresh_button_clicked();
203
void _on_cancel_button_clicked();
204
void _on_close_button_clicked();
205
void _on_result_selected();
206
void _on_item_edited();
207
void _on_replace_text_changed(const String &text);
208
void _on_replace_all_clicked();
209
210
enum {
211
FIND_BUTTON_REPLACE,
212
FIND_BUTTON_REMOVE,
213
};
214
215
struct Result {
216
int line_number = 0;
217
int begin = 0;
218
int end = 0;
219
int begin_trimmed = 0;
220
};
221
222
void apply_replaces_in_file(const String &fpath, const Vector<Result> &locations, const String &new_text);
223
void update_replace_buttons();
224
void update_matches_text();
225
String get_replace_text();
226
227
void draw_result_text(Object *item_obj, Rect2 rect);
228
229
void clear();
230
231
FindInFiles *_finder = nullptr;
232
Label *_find_label = nullptr;
233
Label *_search_text_label = nullptr;
234
Tree *_results_display = nullptr;
235
Label *_status_label = nullptr;
236
CheckButton *_keep_results_button = nullptr;
237
Button *_refresh_button = nullptr;
238
Button *_cancel_button = nullptr;
239
Button *_close_button = nullptr;
240
ProgressBar *_progress_bar = nullptr;
241
HashMap<String, TreeItem *> _file_items;
242
HashMap<TreeItem *, int> _file_items_results_count;
243
HashMap<TreeItem *, Result> _result_items;
244
bool _with_replace = false;
245
246
HBoxContainer *_replace_container = nullptr;
247
LineEdit *_replace_line_edit = nullptr;
248
Button *_replace_all_button = nullptr;
249
250
bool _floating = false;
251
MarginContainer *_results_mc = nullptr;
252
};
253
254
class PopupMenu;
255
class TabContainer;
256
257
// Contains several FindInFilesPanels. A FindInFilesPanel contains the results of a
258
// `Find in Files` search or a `Replace in Files` search, while a
259
// FindInFilesContainer can contain several FindInFilesPanels so that multiple search
260
// results can remain at the same time.
261
class FindInFilesContainer : public EditorDock {
262
GDCLASS(FindInFilesContainer, EditorDock);
263
264
enum {
265
PANEL_CLOSE,
266
PANEL_CLOSE_OTHERS,
267
PANEL_CLOSE_RIGHT,
268
PANEL_CLOSE_ALL,
269
};
270
271
void _on_theme_changed();
272
void _on_tab_close_pressed(int p_tab);
273
void _update_bar_visibility();
274
void _bar_menu_option(int p_option);
275
void _bar_input(const Ref<InputEvent> &p_input);
276
void _on_dock_closed();
277
278
TabContainer *_tabs = nullptr;
279
bool _update_bar = true;
280
PopupMenu *_tabs_context_menu = nullptr;
281
282
FindInFilesPanel *_create_new_panel();
283
FindInFilesPanel *_get_current_panel();
284
285
protected:
286
static void _bind_methods();
287
void _notification(int p_what);
288
289
void _on_find_in_files_result_selected(const String &p_fpath, int p_line_number, int p_begin, int p_end);
290
void _on_find_in_files_modified_files(const PackedStringArray &p_paths);
291
void _on_find_in_files_close_button_clicked(FindInFilesPanel *p_panel);
292
293
virtual void update_layout(EditorDock::DockLayout p_layout) override;
294
295
public:
296
FindInFilesContainer();
297
298
FindInFilesPanel *get_panel_for_results(const String &p_label);
299
};
300
301