Skip to content

Commit 03b6002

Browse files
KoBeWiStronkkey
andcommitted
Allow removing files in the file search
Co-authored-by: Stronkkey <[email protected]>
1 parent d79ff84 commit 03b6002

File tree

2 files changed

+52
-14
lines changed

2 files changed

+52
-14
lines changed

editor/find_in_files.cpp

Lines changed: 50 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -626,6 +626,7 @@ FindInFilesPanel::FindInFilesPanel() {
626626
_results_display->set_v_size_flags(SIZE_EXPAND_FILL);
627627
_results_display->connect(SceneStringName(item_selected), callable_mp(this, &FindInFilesPanel::_on_result_selected));
628628
_results_display->connect("item_edited", callable_mp(this, &FindInFilesPanel::_on_item_edited));
629+
_results_display->connect("button_clicked", callable_mp(this, &FindInFilesPanel::_on_button_clicked));
629630
_results_display->set_hide_root(true);
630631
_results_display->set_select_mode(Tree::SELECT_ROW);
631632
_results_display->set_allow_rmb_select(true);
@@ -733,12 +734,14 @@ void FindInFilesPanel::_notification(int p_what) {
733734

734735
void FindInFilesPanel::_on_result_found(const String &fpath, int line_number, int begin, int end, String text) {
735736
TreeItem *file_item;
736-
HashMap<String, TreeItem *>::Iterator E = _file_items.find(fpath);
737+
Ref<Texture2D> remove_texture = get_editor_theme_icon(SNAME("Close"));
737738

739+
HashMap<String, TreeItem *>::Iterator E = _file_items.find(fpath);
738740
if (!E) {
739741
file_item = _results_display->create_item();
740742
file_item->set_text(0, fpath);
741743
file_item->set_metadata(0, fpath);
744+
file_item->add_button(0, remove_texture, -1, false, TTR("Remove result"));
742745

743746
// The width of this column is restrained to checkboxes,
744747
// 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
781784
item->set_cell_mode(0, TreeItem::CELL_MODE_CHECK);
782785
item->set_checked(0, true);
783786
item->set_editable(0, true);
787+
item->add_button(1, remove_texture, -1, false, TTR("Remove result"));
788+
} else {
789+
item->add_button(0, remove_texture, -1, false, TTR("Remove result"));
784790
}
785791
}
786792

@@ -823,19 +829,7 @@ void FindInFilesPanel::_on_item_edited() {
823829
}
824830

825831
void FindInFilesPanel::_on_finished() {
826-
String results_text;
827-
int result_count = _result_items.size();
828-
int file_count = _file_items.size();
829-
830-
if (result_count == 1 && file_count == 1) {
831-
results_text = vformat(TTR("%d match in %d file"), result_count, file_count);
832-
} else if (result_count != 1 && file_count == 1) {
833-
results_text = vformat(TTR("%d matches in %d file"), result_count, file_count);
834-
} else {
835-
results_text = vformat(TTR("%d matches in %d files"), result_count, file_count);
836-
}
837-
838-
_status_label->set_text(results_text);
832+
update_matches_text();
839833
update_replace_buttons();
840834
set_progress_visible(false);
841835
_refresh_button->show();
@@ -906,6 +900,32 @@ void FindInFilesPanel::_on_replace_all_clicked() {
906900
emit_signal(SNAME(SIGNAL_FILES_MODIFIED), modified_files);
907901
}
908902

903+
void FindInFilesPanel::_on_button_clicked(TreeItem *p_item, int p_column, int p_id, int p_mouse_button_index) {
904+
const String file_path = p_item->get_text(0);
905+
906+
_result_items.erase(p_item);
907+
if (_file_items.find(file_path)) {
908+
TreeItem *file_result = _file_items.get(file_path);
909+
int match_count = file_result->get_child_count();
910+
911+
for (int i = 0; i < match_count; i++) {
912+
TreeItem *child_item = file_result->get_child(i);
913+
_result_items.erase(child_item);
914+
}
915+
916+
file_result->clear_children();
917+
_file_items.erase(file_path);
918+
}
919+
920+
TreeItem *item_parent = p_item->get_parent();
921+
if (item_parent && item_parent->get_child_count() < 2) {
922+
_file_items.erase(item_parent->get_text(0));
923+
get_tree()->queue_delete(item_parent);
924+
}
925+
get_tree()->queue_delete(p_item);
926+
update_matches_text();
927+
}
928+
909929
// Same as get_line, but preserves line ending characters.
910930
class ConservativeGetLine {
911931
public:
@@ -1006,6 +1026,22 @@ void FindInFilesPanel::update_replace_buttons() {
10061026
_replace_all_button->set_disabled(disabled);
10071027
}
10081028

1029+
void FindInFilesPanel::update_matches_text() {
1030+
String results_text;
1031+
int result_count = _result_items.size();
1032+
int file_count = _file_items.size();
1033+
1034+
if (result_count == 1 && file_count == 1) {
1035+
results_text = vformat(TTR("%d match in %d file"), result_count, file_count);
1036+
} else if (result_count != 1 && file_count == 1) {
1037+
results_text = vformat(TTR("%d matches in %d file"), result_count, file_count);
1038+
} else {
1039+
results_text = vformat(TTR("%d matches in %d files"), result_count, file_count);
1040+
}
1041+
1042+
_status_label->set_text(results_text);
1043+
}
1044+
10091045
void FindInFilesPanel::set_progress_visible(bool p_visible) {
10101046
_progress_bar->set_self_modulate(Color(1, 1, 1, p_visible ? 1 : 0));
10111047
}

editor/find_in_files.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,7 @@ class FindInFilesPanel : public Control {
177177
void _notification(int p_what);
178178

179179
private:
180+
void _on_button_clicked(TreeItem *p_item, int p_column, int p_id, int p_mouse_button_index);
180181
void _on_result_found(const String &fpath, int line_number, int begin, int end, String text);
181182
void _on_finished();
182183
void _on_refresh_button_clicked();
@@ -196,6 +197,7 @@ class FindInFilesPanel : public Control {
196197

197198
void apply_replaces_in_file(const String &fpath, const Vector<Result> &locations, const String &new_text);
198199
void update_replace_buttons();
200+
void update_matches_text();
199201
String get_replace_text();
200202

201203
void draw_result_text(Object *item_obj, Rect2 rect);

0 commit comments

Comments
 (0)