diff options
Diffstat (limited to 'editor/find_in_files.cpp')
-rw-r--r-- | editor/find_in_files.cpp | 115 |
1 files changed, 91 insertions, 24 deletions
diff --git a/editor/find_in_files.cpp b/editor/find_in_files.cpp index 4ab90ad3e4..5c012183e7 100644 --- a/editor/find_in_files.cpp +++ b/editor/find_in_files.cpp @@ -47,7 +47,7 @@ const char *FindInFiles::SIGNAL_RESULT_FOUND = "result_found"; const char *FindInFiles::SIGNAL_FINISHED = "finished"; -// TODO Would be nice in Vector and PoolVectors +// TODO Would be nice in Vector and Vectors template <typename T> inline void pop_back(T &container) { container.resize(container.size() - 1); @@ -132,8 +132,8 @@ void FindInFiles::start() { // Init search _current_dir = ""; - PoolStringArray init_folder; - init_folder.append(_root_dir); + PackedStringArray init_folder; + init_folder.push_back(_root_dir); _folders_stack.clear(); _folders_stack.push_back(init_folder); @@ -168,7 +168,7 @@ void FindInFiles::_iterate() { // Scan folders first so we can build a list of files and have progress info later - PoolStringArray &folders_to_scan = _folders_stack.write[_folders_stack.size() - 1]; + PackedStringArray &folders_to_scan = _folders_stack.write[_folders_stack.size() - 1]; if (folders_to_scan.size() != 0) { // Scan one folder below @@ -178,7 +178,7 @@ void FindInFiles::_iterate() { _current_dir = _current_dir.plus_file(folder_name); - PoolStringArray sub_dirs; + PackedStringArray sub_dirs; _scan_dir("res://" + _current_dir, sub_dirs); _folders_stack.push_back(sub_dirs); @@ -219,7 +219,7 @@ float FindInFiles::get_progress() const { return 0; } -void FindInFiles::_scan_dir(String path, PoolStringArray &out_folders) { +void FindInFiles::_scan_dir(String path, PackedStringArray &out_folders) { DirAccessRef dir = DirAccess::open(path); if (!dir) { @@ -235,12 +235,14 @@ void FindInFiles::_scan_dir(String path, PoolStringArray &out_folders) { if (file == "") break; - // Ignore special dirs and hidden dirs (such as .git and .import) + // Ignore special dirs (such as .git and .import) if (file == "." || file == ".." || file.begins_with(".")) continue; + if (dir->current_is_hidden()) + continue; if (dir->current_is_dir()) - out_folders.append(file); + out_folders.push_back(file); else { String file_ext = file.get_extension(); @@ -317,10 +319,21 @@ FindInFilesDialog::FindInFilesDialog() { _search_text_line_edit = memnew(LineEdit); _search_text_line_edit->set_h_size_flags(SIZE_EXPAND_FILL); - _search_text_line_edit->connect("text_changed", this, "_on_search_text_modified"); - _search_text_line_edit->connect("text_entered", this, "_on_search_text_entered"); + _search_text_line_edit->connect_compat("text_changed", this, "_on_search_text_modified"); + _search_text_line_edit->connect_compat("text_entered", this, "_on_search_text_entered"); gc->add_child(_search_text_line_edit); + _replace_label = memnew(Label); + _replace_label->set_text(TTR("Replace:")); + _replace_label->hide(); + gc->add_child(_replace_label); + + _replace_text_line_edit = memnew(LineEdit); + _replace_text_line_edit->set_h_size_flags(SIZE_EXPAND_FILL); + _replace_text_line_edit->connect_compat("text_entered", this, "_on_replace_text_entered"); + _replace_text_line_edit->hide(); + gc->add_child(_replace_text_line_edit); + gc->add_child(memnew(Control)); // Space to maintain the grid aligned. { @@ -354,12 +367,12 @@ FindInFilesDialog::FindInFilesDialog() { Button *folder_button = memnew(Button); folder_button->set_text("..."); - folder_button->connect("pressed", this, "_on_folder_button_pressed"); + folder_button->connect_compat("pressed", this, "_on_folder_button_pressed"); hbc->add_child(folder_button); _folder_dialog = memnew(FileDialog); _folder_dialog->set_mode(FileDialog::MODE_OPEN_DIR); - _folder_dialog->connect("dir_selected", this, "_on_folder_selected"); + _folder_dialog->connect_compat("dir_selected", this, "_on_folder_selected"); add_child(_folder_dialog); gc->add_child(hbc); @@ -381,6 +394,8 @@ FindInFilesDialog::FindInFilesDialog() { Button *cancel_button = get_ok(); cancel_button->set_text(TTR("Cancel")); + + _mode = SEARCH_MODE; } void FindInFilesDialog::set_search_text(String text) { @@ -388,11 +403,40 @@ void FindInFilesDialog::set_search_text(String text) { _on_search_text_modified(text); } +void FindInFilesDialog::set_replace_text(String text) { + _replace_text_line_edit->set_text(text); +} + +void FindInFilesDialog::set_find_in_files_mode(FindInFilesMode p_mode) { + + if (_mode == p_mode) + return; + + _mode = p_mode; + + if (p_mode == SEARCH_MODE) { + set_title(TTR("Find in Files")); + _replace_label->hide(); + _replace_text_line_edit->hide(); + } else if (p_mode == REPLACE_MODE) { + set_title(TTR("Replace in Files")); + _replace_label->show(); + _replace_text_line_edit->show(); + } + + // After hiding some child controls, let's recalculate proper Dialog size + set_size(Size2(get_size().x, 0)); +} + String FindInFilesDialog::get_search_text() const { String text = _search_text_line_edit->get_text(); return text.strip_edges(); } +String FindInFilesDialog::get_replace_text() const { + return _replace_text_line_edit->get_text(); +} + bool FindInFilesDialog::is_match_case() const { return _match_case_checkbox->is_pressed(); } @@ -471,8 +515,26 @@ void FindInFilesDialog::_on_search_text_modified(String text) { void FindInFilesDialog::_on_search_text_entered(String text) { // This allows to trigger a global search without leaving the keyboard - if (!_find_button->is_disabled()) - custom_action("find"); + if (!_find_button->is_disabled()) { + if (_mode == SEARCH_MODE) { + custom_action("find"); + } + } + + if (!_replace_button->is_disabled()) { + if (_mode == REPLACE_MODE) { + custom_action("replace"); + } + } +} + +void FindInFilesDialog::_on_replace_text_entered(String text) { + // This allows to trigger a global search without leaving the keyboard + if (!_replace_button->is_disabled()) { + if (_mode == REPLACE_MODE) { + custom_action("replace"); + } + } } void FindInFilesDialog::_on_folder_selected(String path) { @@ -488,6 +550,7 @@ void FindInFilesDialog::_bind_methods() { ClassDB::bind_method("_on_folder_selected", &FindInFilesDialog::_on_folder_selected); ClassDB::bind_method("_on_search_text_modified", &FindInFilesDialog::_on_search_text_modified); ClassDB::bind_method("_on_search_text_entered", &FindInFilesDialog::_on_search_text_entered); + ClassDB::bind_method("_on_replace_text_entered", &FindInFilesDialog::_on_replace_text_entered); ADD_SIGNAL(MethodInfo(SIGNAL_FIND_REQUESTED)); ADD_SIGNAL(MethodInfo(SIGNAL_REPLACE_REQUESTED)); @@ -500,8 +563,8 @@ const char *FindInFilesPanel::SIGNAL_FILES_MODIFIED = "files_modified"; FindInFilesPanel::FindInFilesPanel() { _finder = memnew(FindInFiles); - _finder->connect(FindInFiles::SIGNAL_RESULT_FOUND, this, "_on_result_found"); - _finder->connect(FindInFiles::SIGNAL_FINISHED, this, "_on_finished"); + _finder->connect_compat(FindInFiles::SIGNAL_RESULT_FOUND, this, "_on_result_found"); + _finder->connect_compat(FindInFiles::SIGNAL_FINISHED, this, "_on_finished"); add_child(_finder); VBoxContainer *vbc = memnew(VBoxContainer); @@ -533,7 +596,7 @@ FindInFilesPanel::FindInFilesPanel() { _cancel_button = memnew(Button); _cancel_button->set_text(TTR("Cancel")); - _cancel_button->connect("pressed", this, "_on_cancel_button_clicked"); + _cancel_button->connect_compat("pressed", this, "_on_cancel_button_clicked"); _cancel_button->hide(); hbc->add_child(_cancel_button); @@ -543,8 +606,8 @@ FindInFilesPanel::FindInFilesPanel() { _results_display = memnew(Tree); _results_display->add_font_override("font", EditorNode::get_singleton()->get_gui_base()->get_font("source", "EditorFonts")); _results_display->set_v_size_flags(SIZE_EXPAND_FILL); - _results_display->connect("item_selected", this, "_on_result_selected"); - _results_display->connect("item_edited", this, "_on_item_edited"); + _results_display->connect_compat("item_selected", this, "_on_result_selected"); + _results_display->connect_compat("item_edited", this, "_on_item_edited"); _results_display->set_hide_root(true); _results_display->set_select_mode(Tree::SELECT_ROW); _results_display->set_allow_rmb_select(true); @@ -562,12 +625,12 @@ FindInFilesPanel::FindInFilesPanel() { _replace_line_edit = memnew(LineEdit); _replace_line_edit->set_h_size_flags(SIZE_EXPAND_FILL); - _replace_line_edit->connect("text_changed", this, "_on_replace_text_changed"); + _replace_line_edit->connect_compat("text_changed", this, "_on_replace_text_changed"); _replace_container->add_child(_replace_line_edit); _replace_all_button = memnew(Button); _replace_all_button->set_text(TTR("Replace all (no undo)")); - _replace_all_button->connect("pressed", this, "_on_replace_all_clicked"); + _replace_all_button->connect_compat("pressed", this, "_on_replace_all_clicked"); _replace_container->add_child(_replace_all_button); _replace_container->hide(); @@ -594,6 +657,10 @@ void FindInFilesPanel::set_with_replace(bool with_replace) { } } +void FindInFilesPanel::set_replace_text(String text) { + _replace_line_edit->set_text(text); +} + void FindInFilesPanel::clear() { _file_items.clear(); _result_items.clear(); @@ -756,7 +823,7 @@ void FindInFilesPanel::_on_replace_all_clicked() { String replace_text = get_replace_text(); - PoolStringArray modified_files; + PackedStringArray modified_files; for (Map<String, TreeItem *>::Element *E = _file_items.front(); E; E = E->next()) { @@ -777,7 +844,7 @@ void FindInFilesPanel::_on_replace_all_clicked() { if (locations.size() != 0) { // Results are sorted by file, so we can batch replaces apply_replaces_in_file(fpath, locations, replace_text); - modified_files.append(fpath); + modified_files.push_back(fpath); } } @@ -884,7 +951,7 @@ void FindInFilesPanel::apply_replaces_in_file(String fpath, const Vector<Result> } String FindInFilesPanel::get_replace_text() { - return _replace_line_edit->get_text().strip_edges(); + return _replace_line_edit->get_text(); } void FindInFilesPanel::update_replace_buttons() { |