Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/editor/script/find_in_files.h
9903 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 "scene/gui/dialogs.h"
35
#include "scene/gui/margin_container.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 Tree;
165
class TreeItem;
166
class ProgressBar;
167
168
// Display search results
169
class FindInFilesPanel : public MarginContainer {
170
GDCLASS(FindInFilesPanel, MarginContainer);
171
172
public:
173
static const char *SIGNAL_RESULT_SELECTED;
174
static const char *SIGNAL_FILES_MODIFIED;
175
static const char *SIGNAL_CLOSE_BUTTON_CLICKED;
176
177
FindInFilesPanel();
178
179
FindInFiles *get_finder() const { return _finder; }
180
181
void set_with_replace(bool with_replace);
182
void set_replace_text(const String &text);
183
184
void start_search();
185
void stop_search();
186
187
protected:
188
static void _bind_methods();
189
190
void _notification(int p_what);
191
192
private:
193
void _on_button_clicked(TreeItem *p_item, int p_column, int p_id, int p_mouse_button_index);
194
void _on_result_found(const String &fpath, int line_number, int begin, int end, String text);
195
void _on_finished();
196
void _on_refresh_button_clicked();
197
void _on_cancel_button_clicked();
198
void _on_close_button_clicked();
199
void _on_result_selected();
200
void _on_item_edited();
201
void _on_replace_text_changed(const String &text);
202
void _on_replace_all_clicked();
203
204
struct Result {
205
int line_number = 0;
206
int begin = 0;
207
int end = 0;
208
int begin_trimmed = 0;
209
};
210
211
void apply_replaces_in_file(const String &fpath, const Vector<Result> &locations, const String &new_text);
212
void update_replace_buttons();
213
void update_matches_text();
214
String get_replace_text();
215
216
void draw_result_text(Object *item_obj, Rect2 rect);
217
218
void clear();
219
220
FindInFiles *_finder = nullptr;
221
Label *_search_text_label = nullptr;
222
Tree *_results_display = nullptr;
223
Label *_status_label = nullptr;
224
Button *_refresh_button = nullptr;
225
Button *_cancel_button = nullptr;
226
Button *_close_button = nullptr;
227
ProgressBar *_progress_bar = nullptr;
228
HashMap<String, TreeItem *> _file_items;
229
HashMap<TreeItem *, Result> _result_items;
230
bool _with_replace = false;
231
232
HBoxContainer *_replace_container = nullptr;
233
LineEdit *_replace_line_edit = nullptr;
234
Button *_replace_all_button = nullptr;
235
};
236
237