<commit_before>using System.Collections.Generic;
using System.Linq;
using UnityEditor;
using UnityEngine;

namespace UnityToolbag
{
    public static class DuplicateWithOriginalName
    {
        private const string MenuItemName = "Edit/Duplicate With Original Name %#d";

        [MenuItem(MenuItemName)]
        public static void Duplicate()
        {
            var newSelection = new List<GameObject>();

            foreach (var origObj in Selection.gameObjects)
            {
                var newObj = Object.Instantiate(origObj);

                // If this object is a prefab instance we need to connect the new object to the prefab
                // and copy over all property modifications so that the instances are identical.
                if (PrefabUtility.GetPrefabType(origObj) == PrefabType.PrefabInstance)
                {
                    var prefab = PrefabUtility.GetPrefabParent(origObj) as GameObject;
                    newObj = PrefabUtility.ConnectGameObjectToPrefab(newObj, prefab);
                    PrefabUtility.SetPropertyModifications(newObj, PrefabUtility.GetPropertyModifications(origObj));
                }

                newObj.name = origObj.name;
                newSelection.Add(newObj);
            }

            Selection.objects = newSelection.Cast<Object>().ToArray();
        }

        [MenuItem(MenuItemName, true)]
        public static bool CanDuplicate()
        {
            if (Selection.objects.Length == 0)
                return false;

            // Don't use our custom duplicate on any assets
            return
                Selection.objects.All(
                    obj =>
                        !AssetDatabase.IsMainAsset(obj) &&
                        !AssetDatabase.IsForeignAsset(obj) &&
                        !AssetDatabase.IsNativeAsset(obj) &&
                        !AssetDatabase.IsSubAsset(obj));
        }
    }
}<commit_msg>Adding undo/redo support for Duplicate With Original Name<commit_after>using System.Collections.Generic;
using System.Linq;
using UnityEditor;
using UnityEngine;

namespace UnityToolbag
{
    public static class DuplicateWithOriginalName
    {
        private const string MenuItemName = "Edit/Duplicate With Original Name %#d";

        [MenuItem(MenuItemName)]
        public static void Duplicate()
        {
            var newSelection = new List<GameObject>();

            foreach (var origObj in Selection.gameObjects)
            {
                var newObj = Object.Instantiate(origObj);

                // If this object is a prefab instance we need to connect the new object to the prefab
                // and copy over all property modifications so that the instances are identical.
                if (PrefabUtility.GetPrefabType(origObj) == PrefabType.PrefabInstance)
                {
                    var prefab = PrefabUtility.GetPrefabParent(origObj) as GameObject;
                    newObj = PrefabUtility.ConnectGameObjectToPrefab(newObj, prefab);
                    PrefabUtility.SetPropertyModifications(newObj, PrefabUtility.GetPropertyModifications(origObj));
                }

                newObj.name = origObj.name;
                newSelection.Add(newObj);

                Undo.RegisterCreatedObjectUndo(newObj, "Duplicate With Original Name");
            }

            Selection.objects = newSelection.Cast<Object>().ToArray();
        }

        [MenuItem(MenuItemName, true)]
        public static bool CanDuplicate()
        {
            if (Selection.objects.Length == 0)
                return false;

            // Don't use our custom duplicate on any assets
            return
                Selection.objects.All(
                    obj =>
                        !AssetDatabase.IsMainAsset(obj) &&
                        !AssetDatabase.IsForeignAsset(obj) &&
                        !AssetDatabase.IsNativeAsset(obj) &&
                        !AssetDatabase.IsSubAsset(obj));
        }
    }
}
{'fraction_non_alphanumeric': 0.07539582809751194, 'fraction_numerical': 0.0005026388539834129, 'mean_word_length': 1.7910238429172511, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4553740', 'n_tokens_mistral': 991, 'n_tokens_neox': 919, 'n_words': 238}