1
0
mirror of https://github.com/godotengine/godot.git synced 2025-11-04 12:00:25 +00:00

Merge pull request #109727 from arkology/replace-individual

`FindInFiles`: Allow replacing individual results
This commit is contained in:
Thaddeus Crews
2025-10-23 15:34:04 -05:00
3 changed files with 42 additions and 6 deletions

View File

@@ -860,11 +860,13 @@ void FindInFilesPanel::_notification(int p_what) {
TreeItem *file_item = _results_display->get_root()->get_first_child();
while (file_item) {
file_item->set_button_tooltip_text(0, 0, TTR("Remove result"));
file_item->set_button_tooltip_text(0, FIND_BUTTON_REPLACE, TTR("Replace all matches in file"));
file_item->set_button_tooltip_text(0, FIND_BUTTON_REMOVE, TTR("Remove result"));
TreeItem *result_item = file_item->get_first_child();
while (result_item) {
result_item->set_button_tooltip_text(_with_replace ? 1 : 0, 0, TTR("Remove result"));
result_item->set_button_tooltip_text(_with_replace ? 1 : 0, FIND_BUTTON_REPLACE, TTR("Replace"));
result_item->set_button_tooltip_text(_with_replace ? 1 : 0, FIND_BUTTON_REMOVE, TTR("Remove result"));
result_item = result_item->get_next();
}
@@ -880,13 +882,18 @@ 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;
Ref<Texture2D> remove_texture = get_editor_theme_icon(SNAME("Close"));
Ref<Texture2D> replace_texture = get_editor_theme_icon(SNAME("ReplaceText"));
HashMap<String, TreeItem *>::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, 0, false, TTR("Remove result"));
if (_with_replace) {
file_item->add_button(0, replace_texture, FIND_BUTTON_REPLACE, false, TTR("Replace all matches in file"));
}
file_item->add_button(0, remove_texture, FIND_BUTTON_REMOVE, false, TTR("Remove result"));
// The width of this column is restrained to checkboxes,
// 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
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, 0, false, TTR("Remove result"));
item->add_button(1, replace_texture, FIND_BUTTON_REPLACE, false, TTR("Replace"));
item->add_button(1, remove_texture, FIND_BUTTON_REMOVE, false, TTR("Remove result"));
} else {
item->add_button(0, remove_texture, 0, false, TTR("Remove result"));
item->add_button(0, remove_texture, FIND_BUTTON_REMOVE, false, TTR("Remove result"));
}
}
@@ -1048,9 +1056,31 @@ void FindInFilesPanel::_on_replace_all_clicked() {
}
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_metadata(0);
if (p_id == FIND_BUTTON_REPLACE) {
const String replace_text = get_replace_text();
Vector<Result> locations;
PackedStringArray modified_files;
if (_file_items.has(file_path)) {
for (TreeItem *item = p_item->get_first_child(); item; item = item->get_next()) {
HashMap<TreeItem *, Result>::Iterator F = _result_items.find(item);
ERR_FAIL_COND(!F);
locations.push_back(F->value);
}
apply_replaces_in_file(file_path, locations, replace_text);
modified_files.push_back(file_path);
} else {
locations.push_back(_result_items.find(p_item)->value);
const String path = p_item->get_parent()->get_metadata(0);
apply_replaces_in_file(path, locations, replace_text);
modified_files.push_back(path);
}
emit_signal(SNAME(SIGNAL_FILES_MODIFIED), modified_files);
}
_result_items.erase(p_item);
if (_file_items_results_count.has(p_item)) {
const String file_path = p_item->get_metadata(0);
int match_count = p_item->get_child_count();
for (int i = 0; i < match_count; i++) {