Repository: microsoft/terminal
File: src/cascadia/TerminalSettingsModel/CascadiaSettings.cpp (C++)

Code before:
{
    auto duplicated{ _userDefaultProfileSettings->CreateChild() };
    _allProfiles.Append(*duplicated);

    const auto newName{ fmt::format(L"{} ({})", source.Name(), RS_(L"CopySuffix")) };
    duplicated->Name(winrt::hstring(newName));

    if (source.HasHidden() || source.HiddenOverrideSource() != nullptr)
    {
        duplicated->Hidden(source.Hidden());
    }
    if (source.HasIcon() || source.IconOverrideSource() != nullptr)
    {
        duplicated->Icon(source.Icon());
    }
    if (source.HasCloseOnExit() || source.CloseOnExitOverrideSource() != nullptr)
    {
        duplicated->CloseOnExit(source.CloseOnExit());
    }
    if (source.HasTabTitle() || source.TabTitleOverrideSource() != nullptr)
    {
        duplicated->TabTitle(source.TabTitle());
    }
    if (source.HasTabColor() || source.TabColorOverrideSource() != nullptr)
    {
        duplicated->TabColor(source.TabColor());
    }
    if (source.HasSuppressApplicationTitle() || source.SuppressApplicationTitleOverrideSource() != nullptr)
    {
        duplicated->SuppressApplicationTitle(source.SuppressApplicationTitle());
    }
    if (source.HasUseAcrylic() || source.UseAcrylicOverrideSource() != nullptr)
    {
        duplicated->UseAcrylic(source.UseAcrylic());
    }
    if (source.HasAcrylicOpacity() || source.AcrylicOpacityOverrideSource() != nullptr)
    {
        duplicated->AcrylicOpacity(source.AcrylicOpacity());
    }
    if (source.HasScrollState() || source.ScrollStateOverrideSource() != nullptr)
    {
        duplicated->ScrollState(source.ScrollState());
    }
    if (source.HasFontFace() || source.FontFaceOverrideSource() != nullptr)
    {
        duplicated->FontFace(source.FontFace());
    }
    if (source.HasFontSize() || source.FontSizeOverrideSource() != nullptr)
    {
        duplicated->FontSize(source.FontSize());
    }

Review comment (refactor):
Is there some way we could add a test for this? To maybe prevent us from forgetting this in the future? 

Maybe like if we did something where we:
* duplicate a profile ("A" -> "A Copy")
* manually change the name of "A Copy" back to "A"
* Call `ToJson` on both profiles and compare the results?

Code after:
        newName = fmt::format(L"Profile {}", _allProfiles.Size() + 1 + candidateIndex);
        if (std::none_of(begin(_allProfiles), end(_allProfiles), [&](auto&& profile) { return profile.Name() == newName; }))
        {
            break;
        }
    }

    auto newProfile{ _userDefaultProfileSettings->CreateChild() };
    newProfile->Name(newName);
    _allProfiles.Append(*newProfile);
    return *newProfile;
}

// Method Description:
// - Duplicate a new profile based off another profile's settings
// - This differs from Profile::Copy because it also copies over settings
//   that were not defined in the json (for example, settings that were
//   defined in one of the parents)
// - This will not duplicate settings that were defined in profiles.defaults
//   however, because we do not want the json blob generated from the new profile
//   to contain those settings
// Arguments:
// - source: the Profile object we are duplicating (must not be null)
// Return Value:
// - a reference to the new profile
winrt::Microsoft::Terminal::Settings::Model::Profile CascadiaSettings::DuplicateProfile(Model::Profile source)
{
    THROW_HR_IF_NULL(E_INVALIDARG, source);

    winrt::com_ptr<Profile> duplicated;
    if (_userDefaultProfileSettings)
    {
        duplicated = _userDefaultProfileSettings->CreateChild();
    }
    else
    {
        duplicated = winrt::make_self<Profile>();
    }
    _allProfiles.Append(*duplicated);

    if (!source.Hidden())
    {
        _activeProfiles.Append(*duplicated);
    }

    winrt::hstring newName{ fmt::format(L"{} ({})", source.Name(), RS_(L"CopySuffix")) };

    // Check if this name already exists and if so, append a number
    for (uint32_t candidateIndex = 0; candidateIndex < _allProfiles.Size() + 1; ++candidateIndex)
    {
        if (std::none_of(begin(_allProfiles), end(_allProfiles), [&](auto&& profile) { return profile.Name() == newName; }))