2023-01-05 20:25:55 +08:00
|
|
|
/**************************************************************************/
|
|
|
|
/* find_in_files.h */
|
|
|
|
/**************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
|
|
|
/* https://godotengine.org */
|
|
|
|
/**************************************************************************/
|
|
|
|
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
|
|
|
|
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* */
|
|
|
|
/* Permission is hereby granted, free of charge, to any person obtaining */
|
|
|
|
/* a copy of this software and associated documentation files (the */
|
|
|
|
/* "Software"), to deal in the Software without restriction, including */
|
|
|
|
/* without limitation the rights to use, copy, modify, merge, publish, */
|
|
|
|
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
|
|
|
/* permit persons to whom the Software is furnished to do so, subject to */
|
|
|
|
/* the following conditions: */
|
|
|
|
/* */
|
|
|
|
/* The above copyright notice and this permission notice shall be */
|
|
|
|
/* included in all copies or substantial portions of the Software. */
|
|
|
|
/* */
|
|
|
|
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
|
|
|
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
|
|
|
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
|
|
|
|
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
|
|
|
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
|
|
|
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
|
|
|
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
|
|
|
/**************************************************************************/
|
2018-02-12 09:36:15 +08:00
|
|
|
|
|
|
|
#ifndef FIND_IN_FILES_H
|
|
|
|
#define FIND_IN_FILES_H
|
|
|
|
|
2020-11-08 06:33:38 +08:00
|
|
|
#include "core/templates/hash_map.h"
|
2018-02-12 09:36:15 +08:00
|
|
|
#include "scene/gui/dialogs.h"
|
|
|
|
|
|
|
|
// Performs the actual search
|
|
|
|
class FindInFiles : public Node {
|
2019-03-20 02:35:57 +08:00
|
|
|
GDCLASS(FindInFiles, Node);
|
|
|
|
|
2018-02-12 09:36:15 +08:00
|
|
|
public:
|
|
|
|
static const char *SIGNAL_RESULT_FOUND;
|
|
|
|
static const char *SIGNAL_FINISHED;
|
|
|
|
|
2024-02-16 00:25:58 +08:00
|
|
|
void set_search_text(const String &p_pattern);
|
2018-02-12 09:36:15 +08:00
|
|
|
void set_whole_words(bool p_whole_word);
|
|
|
|
void set_match_case(bool p_match_case);
|
2024-02-16 00:25:58 +08:00
|
|
|
void set_folder(const String &folder);
|
2022-05-19 23:00:06 +08:00
|
|
|
void set_filter(const HashSet<String> &exts);
|
2018-02-12 09:36:15 +08:00
|
|
|
|
|
|
|
String get_search_text() const { return _pattern; }
|
|
|
|
|
|
|
|
bool is_whole_words() const { return _whole_words; }
|
|
|
|
bool is_match_case() const { return _match_case; }
|
|
|
|
|
|
|
|
void start();
|
|
|
|
void stop();
|
|
|
|
|
|
|
|
bool is_searching() const { return _searching; }
|
|
|
|
float get_progress() const;
|
|
|
|
|
|
|
|
protected:
|
2022-02-16 07:52:32 +08:00
|
|
|
void _notification(int p_what);
|
2018-02-12 09:36:15 +08:00
|
|
|
|
|
|
|
static void _bind_methods();
|
|
|
|
|
|
|
|
private:
|
|
|
|
void _process();
|
|
|
|
void _iterate();
|
2024-02-16 00:25:58 +08:00
|
|
|
void _scan_dir(const String &path, PackedStringArray &out_folders, PackedStringArray &out_files_to_scan);
|
|
|
|
void _scan_file(const String &fpath);
|
2018-02-12 09:36:15 +08:00
|
|
|
|
|
|
|
// Config
|
|
|
|
String _pattern;
|
2022-05-19 23:00:06 +08:00
|
|
|
HashSet<String> _extension_filter;
|
2018-02-12 09:36:15 +08:00
|
|
|
String _root_dir;
|
2020-11-24 17:12:55 +08:00
|
|
|
bool _whole_words = true;
|
|
|
|
bool _match_case = true;
|
2018-02-12 09:36:15 +08:00
|
|
|
|
|
|
|
// State
|
2020-11-24 17:12:55 +08:00
|
|
|
bool _searching = false;
|
2018-02-12 09:36:15 +08:00
|
|
|
String _current_dir;
|
2020-02-18 05:06:54 +08:00
|
|
|
Vector<PackedStringArray> _folders_stack;
|
2018-02-12 09:36:15 +08:00
|
|
|
Vector<String> _files_to_scan;
|
2020-11-24 17:12:55 +08:00
|
|
|
int _initial_files_count = 0;
|
2018-02-12 09:36:15 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
class LineEdit;
|
|
|
|
class CheckBox;
|
|
|
|
class FileDialog;
|
2019-02-05 05:59:51 +08:00
|
|
|
class HBoxContainer;
|
2018-02-12 09:36:15 +08:00
|
|
|
|
|
|
|
// Prompts search parameters
|
2018-09-18 22:10:36 +08:00
|
|
|
class FindInFilesDialog : public AcceptDialog {
|
2019-03-20 02:35:57 +08:00
|
|
|
GDCLASS(FindInFilesDialog, AcceptDialog);
|
|
|
|
|
2018-02-12 09:36:15 +08:00
|
|
|
public:
|
2020-02-09 17:10:58 +08:00
|
|
|
enum FindInFilesMode {
|
|
|
|
SEARCH_MODE,
|
|
|
|
REPLACE_MODE
|
|
|
|
};
|
|
|
|
|
2018-02-12 09:36:15 +08:00
|
|
|
static const char *SIGNAL_FIND_REQUESTED;
|
|
|
|
static const char *SIGNAL_REPLACE_REQUESTED;
|
|
|
|
|
|
|
|
FindInFilesDialog();
|
|
|
|
|
2024-02-16 00:25:58 +08:00
|
|
|
void set_search_text(const String &text);
|
|
|
|
void set_replace_text(const String &text);
|
2020-02-09 17:10:58 +08:00
|
|
|
|
|
|
|
void set_find_in_files_mode(FindInFilesMode p_mode);
|
2018-02-12 09:36:15 +08:00
|
|
|
|
|
|
|
String get_search_text() const;
|
2020-02-09 17:10:58 +08:00
|
|
|
String get_replace_text() const;
|
2018-02-12 09:36:15 +08:00
|
|
|
bool is_match_case() const;
|
|
|
|
bool is_whole_words() const;
|
|
|
|
String get_folder() const;
|
2022-05-19 23:00:06 +08:00
|
|
|
HashSet<String> get_filter() const;
|
2018-02-12 09:36:15 +08:00
|
|
|
|
|
|
|
protected:
|
|
|
|
void _notification(int p_what);
|
2020-03-07 01:00:16 +08:00
|
|
|
|
|
|
|
void _visibility_changed();
|
2020-07-10 18:34:39 +08:00
|
|
|
void custom_action(const String &p_action) override;
|
2020-03-07 01:00:16 +08:00
|
|
|
static void _bind_methods();
|
2018-02-12 09:36:15 +08:00
|
|
|
|
|
|
|
private:
|
|
|
|
void _on_folder_button_pressed();
|
|
|
|
void _on_folder_selected(String path);
|
2024-02-16 00:25:58 +08:00
|
|
|
void _on_search_text_modified(const String &text);
|
|
|
|
void _on_search_text_submitted(const String &text);
|
|
|
|
void _on_replace_text_submitted(const String &text);
|
2018-02-12 09:36:15 +08:00
|
|
|
|
2020-02-09 17:10:58 +08:00
|
|
|
FindInFilesMode _mode;
|
2022-04-04 21:06:57 +08:00
|
|
|
LineEdit *_search_text_line_edit = nullptr;
|
|
|
|
|
|
|
|
Label *_replace_label = nullptr;
|
|
|
|
LineEdit *_replace_text_line_edit = nullptr;
|
|
|
|
|
|
|
|
LineEdit *_folder_line_edit = nullptr;
|
|
|
|
CheckBox *_match_case_checkbox = nullptr;
|
|
|
|
CheckBox *_whole_words_checkbox = nullptr;
|
|
|
|
Button *_find_button = nullptr;
|
|
|
|
Button *_replace_button = nullptr;
|
|
|
|
FileDialog *_folder_dialog = nullptr;
|
|
|
|
HBoxContainer *_filters_container = nullptr;
|
2019-02-05 05:59:51 +08:00
|
|
|
HashMap<String, bool> _filters_preferences;
|
2018-02-12 09:36:15 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
class Button;
|
2018-08-19 07:36:44 +08:00
|
|
|
class Tree;
|
|
|
|
class TreeItem;
|
2018-02-12 09:36:15 +08:00
|
|
|
class ProgressBar;
|
|
|
|
|
|
|
|
// Display search results
|
|
|
|
class FindInFilesPanel : public Control {
|
2019-03-20 02:35:57 +08:00
|
|
|
GDCLASS(FindInFilesPanel, Control);
|
|
|
|
|
2018-02-12 09:36:15 +08:00
|
|
|
public:
|
|
|
|
static const char *SIGNAL_RESULT_SELECTED;
|
|
|
|
static const char *SIGNAL_FILES_MODIFIED;
|
2024-02-25 22:40:39 +08:00
|
|
|
static const char *SIGNAL_CLOSE_BUTTON_CLICKED;
|
2018-02-12 09:36:15 +08:00
|
|
|
|
|
|
|
FindInFilesPanel();
|
|
|
|
|
|
|
|
FindInFiles *get_finder() const { return _finder; }
|
|
|
|
|
|
|
|
void set_with_replace(bool with_replace);
|
2024-02-16 00:25:58 +08:00
|
|
|
void set_replace_text(const String &text);
|
2018-02-12 09:36:15 +08:00
|
|
|
|
|
|
|
void start_search();
|
|
|
|
void stop_search();
|
|
|
|
|
|
|
|
protected:
|
|
|
|
static void _bind_methods();
|
|
|
|
|
|
|
|
void _notification(int p_what);
|
|
|
|
|
|
|
|
private:
|
2024-02-16 00:25:58 +08:00
|
|
|
void _on_result_found(const String &fpath, int line_number, int begin, int end, String text);
|
2018-02-12 09:36:15 +08:00
|
|
|
void _on_finished();
|
2020-02-10 21:04:15 +08:00
|
|
|
void _on_refresh_button_clicked();
|
2018-02-12 09:36:15 +08:00
|
|
|
void _on_cancel_button_clicked();
|
2024-02-25 22:40:39 +08:00
|
|
|
void _on_close_button_clicked();
|
2018-08-19 07:36:44 +08:00
|
|
|
void _on_result_selected();
|
|
|
|
void _on_item_edited();
|
2024-02-16 00:25:58 +08:00
|
|
|
void _on_replace_text_changed(const String &text);
|
2018-02-12 09:36:15 +08:00
|
|
|
void _on_replace_all_clicked();
|
|
|
|
|
2018-08-19 07:36:44 +08:00
|
|
|
struct Result {
|
2020-11-24 17:12:55 +08:00
|
|
|
int line_number = 0;
|
|
|
|
int begin = 0;
|
|
|
|
int end = 0;
|
|
|
|
int begin_trimmed = 0;
|
2018-08-19 07:36:44 +08:00
|
|
|
};
|
2018-02-12 09:36:15 +08:00
|
|
|
|
2024-02-16 00:25:58 +08:00
|
|
|
void apply_replaces_in_file(const String &fpath, const Vector<Result> &locations, const String &new_text);
|
2018-02-12 09:36:15 +08:00
|
|
|
void update_replace_buttons();
|
|
|
|
String get_replace_text();
|
2018-08-19 07:36:44 +08:00
|
|
|
|
|
|
|
void draw_result_text(Object *item_obj, Rect2 rect);
|
|
|
|
|
2022-09-29 17:53:28 +08:00
|
|
|
void set_progress_visible(bool p_visible);
|
2018-08-19 07:36:44 +08:00
|
|
|
void clear();
|
2018-02-12 09:36:15 +08:00
|
|
|
|
2022-04-04 21:06:57 +08:00
|
|
|
FindInFiles *_finder = nullptr;
|
|
|
|
Label *_search_text_label = nullptr;
|
|
|
|
Tree *_results_display = nullptr;
|
|
|
|
Label *_status_label = nullptr;
|
|
|
|
Button *_refresh_button = nullptr;
|
|
|
|
Button *_cancel_button = nullptr;
|
2024-02-25 22:40:39 +08:00
|
|
|
Button *_close_button = nullptr;
|
2022-04-04 21:06:57 +08:00
|
|
|
ProgressBar *_progress_bar = nullptr;
|
2022-05-13 21:04:37 +08:00
|
|
|
HashMap<String, TreeItem *> _file_items;
|
|
|
|
HashMap<TreeItem *, Result> _result_items;
|
2022-02-15 22:56:58 +08:00
|
|
|
bool _with_replace = false;
|
2018-02-12 09:36:15 +08:00
|
|
|
|
2022-04-04 21:06:57 +08:00
|
|
|
HBoxContainer *_replace_container = nullptr;
|
|
|
|
LineEdit *_replace_line_edit = nullptr;
|
|
|
|
Button *_replace_all_button = nullptr;
|
2018-02-12 09:36:15 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif // FIND_IN_FILES_H
|