From 03b60027eea49165268fa94aabb6cbd3e8ec6a67 Mon Sep 17 00:00:00 2001 From: kobewi Date: Sun, 12 Jan 2025 23:24:15 +0100 Subject: [PATCH] Allow removing files in the file search Co-authored-by: Stronkkey --- editor/find_in_files.cpp | 64 +++++++++++++++++++++++++++++++--------- editor/find_in_files.h | 2 ++ 2 files changed, 52 insertions(+), 14 deletions(-) diff --git a/editor/find_in_files.cpp b/editor/find_in_files.cpp index 42f4b59186c..cc3f3f2259d 100644 --- a/editor/find_in_files.cpp +++ b/editor/find_in_files.cpp @@ -626,6 +626,7 @@ FindInFilesPanel::FindInFilesPanel() { _results_display->set_v_size_flags(SIZE_EXPAND_FILL); _results_display->connect(SceneStringName(item_selected), callable_mp(this, &FindInFilesPanel::_on_result_selected)); _results_display->connect("item_edited", callable_mp(this, &FindInFilesPanel::_on_item_edited)); + _results_display->connect("button_clicked", callable_mp(this, &FindInFilesPanel::_on_button_clicked)); _results_display->set_hide_root(true); _results_display->set_select_mode(Tree::SELECT_ROW); _results_display->set_allow_rmb_select(true); @@ -733,12 +734,14 @@ void FindInFilesPanel::_notification(int p_what) { void FindInFilesPanel::_on_result_found(const String &fpath, int line_number, int begin, int end, String text) { TreeItem *file_item; - HashMap::Iterator E = _file_items.find(fpath); + Ref remove_texture = get_editor_theme_icon(SNAME("Close")); + HashMap::Iterator E = _file_items.find(fpath); if (!E) { file_item = _results_display->create_item(); file_item->set_text(0, fpath); file_item->set_metadata(0, fpath); + file_item->add_button(0, remove_texture, -1, false, TTR("Remove result")); // The width of this column is restrained to checkboxes, // but that doesn't make sense for the parent items, @@ -781,6 +784,9 @@ void FindInFilesPanel::_on_result_found(const String &fpath, int line_number, in item->set_cell_mode(0, TreeItem::CELL_MODE_CHECK); item->set_checked(0, true); item->set_editable(0, true); + item->add_button(1, remove_texture, -1, false, TTR("Remove result")); + } else { + item->add_button(0, remove_texture, -1, false, TTR("Remove result")); } } @@ -823,19 +829,7 @@ void FindInFilesPanel::_on_item_edited() { } void FindInFilesPanel::_on_finished() { - String results_text; - int result_count = _result_items.size(); - int file_count = _file_items.size(); - - if (result_count == 1 && file_count == 1) { - results_text = vformat(TTR("%d match in %d file"), result_count, file_count); - } else if (result_count != 1 && file_count == 1) { - results_text = vformat(TTR("%d matches in %d file"), result_count, file_count); - } else { - results_text = vformat(TTR("%d matches in %d files"), result_count, file_count); - } - - _status_label->set_text(results_text); + update_matches_text(); update_replace_buttons(); set_progress_visible(false); _refresh_button->show(); @@ -906,6 +900,32 @@ void FindInFilesPanel::_on_replace_all_clicked() { emit_signal(SNAME(SIGNAL_FILES_MODIFIED), modified_files); } +void FindInFilesPanel::_on_button_clicked(TreeItem *p_item, int p_column, int p_id, int p_mouse_button_index) { + const String file_path = p_item->get_text(0); + + _result_items.erase(p_item); + if (_file_items.find(file_path)) { + TreeItem *file_result = _file_items.get(file_path); + int match_count = file_result->get_child_count(); + + for (int i = 0; i < match_count; i++) { + TreeItem *child_item = file_result->get_child(i); + _result_items.erase(child_item); + } + + file_result->clear_children(); + _file_items.erase(file_path); + } + + TreeItem *item_parent = p_item->get_parent(); + if (item_parent && item_parent->get_child_count() < 2) { + _file_items.erase(item_parent->get_text(0)); + get_tree()->queue_delete(item_parent); + } + get_tree()->queue_delete(p_item); + update_matches_text(); +} + // Same as get_line, but preserves line ending characters. class ConservativeGetLine { public: @@ -1006,6 +1026,22 @@ void FindInFilesPanel::update_replace_buttons() { _replace_all_button->set_disabled(disabled); } +void FindInFilesPanel::update_matches_text() { + String results_text; + int result_count = _result_items.size(); + int file_count = _file_items.size(); + + if (result_count == 1 && file_count == 1) { + results_text = vformat(TTR("%d match in %d file"), result_count, file_count); + } else if (result_count != 1 && file_count == 1) { + results_text = vformat(TTR("%d matches in %d file"), result_count, file_count); + } else { + results_text = vformat(TTR("%d matches in %d files"), result_count, file_count); + } + + _status_label->set_text(results_text); +} + void FindInFilesPanel::set_progress_visible(bool p_visible) { _progress_bar->set_self_modulate(Color(1, 1, 1, p_visible ? 1 : 0)); } diff --git a/editor/find_in_files.h b/editor/find_in_files.h index ac336b4e358..c3760ed5208 100644 --- a/editor/find_in_files.h +++ b/editor/find_in_files.h @@ -177,6 +177,7 @@ protected: void _notification(int p_what); private: + void _on_button_clicked(TreeItem *p_item, int p_column, int p_id, int p_mouse_button_index); void _on_result_found(const String &fpath, int line_number, int begin, int end, String text); void _on_finished(); void _on_refresh_button_clicked(); @@ -196,6 +197,7 @@ private: void apply_replaces_in_file(const String &fpath, const Vector &locations, const String &new_text); void update_replace_buttons(); + void update_matches_text(); String get_replace_text(); void draw_result_text(Object *item_obj, Rect2 rect);