瀏覽代碼

Merge pull request #109727 from arkology/replace-individual

`FindInFiles`: Allow replacing individual results
Thaddeus Crews 1 月之前
父節點
當前提交
084d5d407e
共有 3 個文件被更改,包括 42 次插入6 次删除
  1. 1 0
      editor/icons/ReplaceText.svg
  2. 36 6
      editor/script/find_in_files.cpp
  3. 5 0
      editor/script/find_in_files.h

+ 1 - 0
editor/icons/ReplaceText.svg

@@ -0,0 +1 @@
+<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16"><path fill="#e0e0e0" d="M14.5 2a1 1 0 00-1.715-.697 3 3 0 100 5.392 1 1 0 001.715-.695zm-3 .5a1 1 0 010 3 1 1 0 010-3ZM3.5 4.5l2 2 2-2-.5-.5-1 1v-1.5a.523.523 0 01.5-.526h1.52v-.965h-1.52a1.482 1.482 0 00-1.48 1.489v1.502l-1.012-1.009zM2.75 7.25a1 1 0 00-1 1v6a1 1 0 001 1h6a1 1 0 001-1v-6a1 1 0 00-1-1zm0 1h6v6h-6zm4.5.5v1.5h-1a1 1 0 000 2h1v1.5h-1a1 1 0 010-5z"/></svg>

+ 36 - 6
editor/script/find_in_files.cpp

@@ -860,11 +860,13 @@ void FindInFilesPanel::_notification(int p_what) {
 
 
 			TreeItem *file_item = _results_display->get_root()->get_first_child();
 			TreeItem *file_item = _results_display->get_root()->get_first_child();
 			while (file_item) {
 			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();
 				TreeItem *result_item = file_item->get_first_child();
 				while (result_item) {
 				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();
 					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) {
 void FindInFilesPanel::_on_result_found(const String &fpath, int line_number, int begin, int end, String text) {
 	TreeItem *file_item;
 	TreeItem *file_item;
 	Ref<Texture2D> remove_texture = get_editor_theme_icon(SNAME("Close"));
 	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);
 	HashMap<String, TreeItem *>::Iterator E = _file_items.find(fpath);
 	if (!E) {
 	if (!E) {
 		file_item = _results_display->create_item();
 		file_item = _results_display->create_item();
 		file_item->set_text(0, fpath);
 		file_item->set_text(0, fpath);
 		file_item->set_metadata(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,
 		// The width of this column is restrained to checkboxes,
 		// but that doesn't make sense for the parent items,
 		// 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_cell_mode(0, TreeItem::CELL_MODE_CHECK);
 		item->set_checked(0, true);
 		item->set_checked(0, true);
 		item->set_editable(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 {
 	} 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) {
 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);
 	_result_items.erase(p_item);
 	if (_file_items_results_count.has(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();
 		int match_count = p_item->get_child_count();
 
 
 		for (int i = 0; i < match_count; i++) {
 		for (int i = 0; i < match_count; i++) {

+ 5 - 0
editor/script/find_in_files.h

@@ -201,6 +201,11 @@ private:
 	void _on_replace_text_changed(const String &text);
 	void _on_replace_text_changed(const String &text);
 	void _on_replace_all_clicked();
 	void _on_replace_all_clicked();
 
 
+	enum {
+		FIND_BUTTON_REPLACE,
+		FIND_BUTTON_REMOVE,
+	};
+
 	struct Result {
 	struct Result {
 		int line_number = 0;
 		int line_number = 0;
 		int begin = 0;
 		int begin = 0;