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

Code before:
    _allProfiles.Append(*duplicated);

    if (!source.Hidden())
    {
        _activeProfiles.Append(*duplicated);
    }

    const auto newName{ fmt::format(L"{} ({})", source.Name(), RS_(L"CopySuffix")) };
    duplicated->Name(winrt::hstring(newName));

    #define DUPLICATE_MACRO(settingName)                                                                                                                            \
    if (source.Has##settingName() ||                                                                                                                                \
       (source.##settingName##OverrideSource() != nullptr && source.##settingName##OverrideSource().Origin() != OriginTag::UserDefaults))                           \
    {                                                                                                                                                               \
        duplicated->##settingName(source.##settingName());                                                                                                          \
    }

    DUPLICATE_MACRO(Hidden);
    DUPLICATE_MACRO(Icon);
    DUPLICATE_MACRO(CloseOnExit);
    DUPLICATE_MACRO(TabTitle);
    DUPLICATE_MACRO(TabColor);
    DUPLICATE_MACRO(SuppressApplicationTitle);
    DUPLICATE_MACRO(UseAcrylic);
    DUPLICATE_MACRO(AcrylicOpacity);
    DUPLICATE_MACRO(ScrollState);
    DUPLICATE_MACRO(FontFace);
    DUPLICATE_MACRO(FontSize);
    DUPLICATE_MACRO(FontWeight);
    DUPLICATE_MACRO(Padding);
    DUPLICATE_MACRO(Commandline);
    DUPLICATE_MACRO(StartingDirectory);
    DUPLICATE_MACRO(BackgroundImagePath);
    DUPLICATE_MACRO(BackgroundImageOpacity);
    DUPLICATE_MACRO(BackgroundImageStretchMode);
    DUPLICATE_MACRO(AntialiasingMode);
    DUPLICATE_MACRO(RetroTerminalEffect);
    DUPLICATE_MACRO(ForceFullRepaintRendering);
    DUPLICATE_MACRO(SoftwareRendering);
    DUPLICATE_MACRO(ColorSchemeName);
    DUPLICATE_MACRO(Foreground);
    DUPLICATE_MACRO(Background);
    DUPLICATE_MACRO(SelectionBackground);
    DUPLICATE_MACRO(CursorColor);
    DUPLICATE_MACRO(HistorySize);
    DUPLICATE_MACRO(SnapOnInput);
    DUPLICATE_MACRO(AltGrAliasing);
    DUPLICATE_MACRO(CursorShape);
    DUPLICATE_MACRO(CursorHeight);
    DUPLICATE_MACRO(BellStyle);
    DUPLICATE_MACRO(PixelShaderPath);

Review comment (refactor):
for @zadjii-msft or @carlos-zamora to re-review -- this is supposed to duplicate all settings that are _not_ set in BaseLayer -- it will promote defaults.json, Generated, Fragment or User-level settings into the new profile block, which I believe is correct.

Code after:

    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; }))
        {
            break;
        }
        // There is a theoretical unsigned integer wraparound, which is OK
        newName = fmt::format(L"{} ({} {})", source.Name(), RS_(L"CopySuffix"), candidateIndex + 2);
    }