Skip to content

Commit 084d5d4

Browse files
committed
Merge pull request #109727 from arkology/replace-individual
`FindInFiles`: Allow replacing individual results
2 parents add458d + 221f1f4 commit 084d5d4

File tree

3 files changed

+42
-6
lines changed

3 files changed

+42
-6
lines changed

editor/icons/ReplaceText.svg

Lines changed: 1 addition & 0 deletions
Loading

editor/script/find_in_files.cpp

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -860,11 +860,13 @@ void FindInFilesPanel::_notification(int p_what) {
860860

861861
TreeItem *file_item = _results_display->get_root()->get_first_child();
862862
while (file_item) {
863-
file_item->set_button_tooltip_text(0, 0, TTR("Remove result"));
863+
file_item->set_button_tooltip_text(0, FIND_BUTTON_REPLACE, TTR("Replace all matches in file"));
864+
file_item->set_button_tooltip_text(0, FIND_BUTTON_REMOVE, TTR("Remove result"));
864865

865866
TreeItem *result_item = file_item->get_first_child();
866867
while (result_item) {
867-
result_item->set_button_tooltip_text(_with_replace ? 1 : 0, 0, TTR("Remove result"));
868+
result_item->set_button_tooltip_text(_with_replace ? 1 : 0, FIND_BUTTON_REPLACE, TTR("Replace"));
869+
result_item->set_button_tooltip_text(_with_replace ? 1 : 0, FIND_BUTTON_REMOVE, TTR("Remove result"));
868870
result_item = result_item->get_next();
869871
}
870872

@@ -880,13 +882,18 @@ void FindInFilesPanel::_notification(int p_what) {
880882
void FindInFilesPanel::_on_result_found(const String &fpath, int line_number, int begin, int end, String text) {
881883
TreeItem *file_item;
882884
Ref<Texture2D> remove_texture = get_editor_theme_icon(SNAME("Close"));
885+
Ref<Texture2D> replace_texture = get_editor_theme_icon(SNAME("ReplaceText"));
883886

884887
HashMap<String, TreeItem *>::Iterator E = _file_items.find(fpath);
885888
if (!E) {
886889
file_item = _results_display->create_item();
887890
file_item->set_text(0, fpath);
888891
file_item->set_metadata(0, fpath);
889-
file_item->add_button(0, remove_texture, 0, false, TTR("Remove result"));
892+
893+
if (_with_replace) {
894+
file_item->add_button(0, replace_texture, FIND_BUTTON_REPLACE, false, TTR("Replace all matches in file"));
895+
}
896+
file_item->add_button(0, remove_texture, FIND_BUTTON_REMOVE, false, TTR("Remove result"));
890897

891898
// The width of this column is restrained to checkboxes,
892899
// but that doesn't make sense for the parent items,
@@ -931,9 +938,10 @@ void FindInFilesPanel::_on_result_found(const String &fpath, int line_number, in
931938
item->set_cell_mode(0, TreeItem::CELL_MODE_CHECK);
932939
item->set_checked(0, true);
933940
item->set_editable(0, true);
934-
item->add_button(1, remove_texture, 0, false, TTR("Remove result"));
941+
item->add_button(1, replace_texture, FIND_BUTTON_REPLACE, false, TTR("Replace"));
942+
item->add_button(1, remove_texture, FIND_BUTTON_REMOVE, false, TTR("Remove result"));
935943
} else {
936-
item->add_button(0, remove_texture, 0, false, TTR("Remove result"));
944+
item->add_button(0, remove_texture, FIND_BUTTON_REMOVE, false, TTR("Remove result"));
937945
}
938946
}
939947

@@ -1048,9 +1056,31 @@ void FindInFilesPanel::_on_replace_all_clicked() {
10481056
}
10491057

10501058
void FindInFilesPanel::_on_button_clicked(TreeItem *p_item, int p_column, int p_id, int p_mouse_button_index) {
1059+
const String file_path = p_item->get_metadata(0);
1060+
1061+
if (p_id == FIND_BUTTON_REPLACE) {
1062+
const String replace_text = get_replace_text();
1063+
Vector<Result> locations;
1064+
PackedStringArray modified_files;
1065+
if (_file_items.has(file_path)) {
1066+
for (TreeItem *item = p_item->get_first_child(); item; item = item->get_next()) {
1067+
HashMap<TreeItem *, Result>::Iterator F = _result_items.find(item);
1068+
ERR_FAIL_COND(!F);
1069+
locations.push_back(F->value);
1070+
}
1071+
apply_replaces_in_file(file_path, locations, replace_text);
1072+
modified_files.push_back(file_path);
1073+
} else {
1074+
locations.push_back(_result_items.find(p_item)->value);
1075+
const String path = p_item->get_parent()->get_metadata(0);
1076+
apply_replaces_in_file(path, locations, replace_text);
1077+
modified_files.push_back(path);
1078+
}
1079+
emit_signal(SNAME(SIGNAL_FILES_MODIFIED), modified_files);
1080+
}
1081+
10511082
_result_items.erase(p_item);
10521083
if (_file_items_results_count.has(p_item)) {
1053-
const String file_path = p_item->get_metadata(0);
10541084
int match_count = p_item->get_child_count();
10551085

10561086
for (int i = 0; i < match_count; i++) {

editor/script/find_in_files.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,11 @@ class FindInFilesPanel : public MarginContainer {
201201
void _on_replace_text_changed(const String &text);
202202
void _on_replace_all_clicked();
203203

204+
enum {
205+
FIND_BUTTON_REPLACE,
206+
FIND_BUTTON_REMOVE,
207+
};
208+
204209
struct Result {
205210
int line_number = 0;
206211
int begin = 0;

0 commit comments

Comments
 (0)