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

Code before:
            continue;
        }
        themes.append(theme->ToJson());
    }
    json[JsonKey(ThemesKey)] = themes;

    return json;
}

std::string_view CascadiaSettings::_getDefaultsJsonHelper()
{
    const auto resource = FindResourceW(wil::GetModuleInstanceHandle(), MAKEINTRESOURCEW(IDR_DEFAULTS), RT_RCDATA);
    const auto loaded = LoadResource(wil::GetModuleInstanceHandle(), resource);
    const auto sz = SizeofResource(wil::GetModuleInstanceHandle(), resource);
    const auto ptr = LockResource(loaded);
    return { reinterpret_cast<const char*>(ptr), sz };
}

std::string_view CascadiaSettings::_getUserDefaultsJsonHelper()
{
    const auto resource = FindResourceW(wil::GetModuleInstanceHandle(), MAKEINTRESOURCEW(IDR_USER_DEFAULTS), RT_RCDATA);
    const auto loaded = LoadResource(wil::GetModuleInstanceHandle(), resource);
    const auto sz = SizeofResource(wil::GetModuleInstanceHandle(), resource);
    const auto ptr = LockResource(loaded);
    return { reinterpret_cast<const char*>(ptr), sz };
}

// Method Description:
// - Resolves the "defaultProfile", which can be a profile name, to a GUID
//   and stores it back to the globals.
void CascadiaSettings::_resolveDefaultProfile() const
{
    if (const auto unparsedDefaultProfile = _globals->UnparsedDefaultProfile(); !unparsedDefaultProfile.empty())
    {
        if (const auto profile = GetProfileByName(unparsedDefaultProfile))
        {
            _globals->DefaultProfile(profile.Guid());
            return;
        }

        _warnings.Append(SettingsLoadWarnings::MissingDefaultProfile);
    }

    // Use the first profile as the new default.
    GlobalSettings().DefaultProfile(_allProfiles.GetAt(0).Guid());
}

// Method Description:
// - Iterates through the "newTabMenu" entries and for ProfileEntries resolves the "profile"
//   fields, which can be a profile name, to a GUID and stores it back.
// - It finds any "source" entries and finds all profiles generated by that source

Review comment (suggestion):
If we had a `std::string_view _loadStringResource(int id)` function we could replace these 3 copies with just (respectively)
```cpp
_loadStringResource(IDR_DEFAULTS);
_loadStringResource(IDR_USER_DEFAULTS);
_loadStringResource(IDR_ENABLE_COLOR_SELECTION);
```

Also, I believe it'd be better to save the return value from `wil::GetModuleInstanceHandle()`. It's an external function and it's "unknown" how complex it is.

Code after:
// Method Description:
// - Resolves the "defaultProfile", which can be a profile name, to a GUID
//   and stores it back to the globals.
void CascadiaSettings::_resolveDefaultProfile() const
{
    if (const auto unparsedDefaultProfile = _globals->UnparsedDefaultProfile(); !unparsedDefaultProfile.empty())
    {
        if (const auto profile = GetProfileByName(unparsedDefaultProfile))
        {
            _globals->DefaultProfile(profile.Guid());
            return;
        }

        _warnings.Append(SettingsLoadWarnings::MissingDefaultProfile);
    }

    // Use the first profile as the new default.
    GlobalSettings().DefaultProfile(_allProfiles.GetAt(0).Guid());
}

// Method Description:
// - Iterates through the "newTabMenu" entries and for ProfileEntries resolves the "profile"
//   fields, which can be a profile name, to a GUID and stores it back.
// - It finds any "source" entries and finds all profiles generated by that source
// - Lastly, it finds any "remainingProfiles" entries and stores which profiles they
//   represent (those that were not resolved before). It adds a warning when
//   multiple of these entries are found.
void CascadiaSettings::_resolveNewTabMenuProfiles() const
{
    Model::RemainingProfilesEntry remainingProfilesEntry = nullptr;

    // The TerminalPage needs to know which profile has which profile ID. To prevent
    // continuous lookups in the _activeProfiles vector, we create a map <int, Profile>
    // to store these indices in-flight.
    auto remainingProfilesMap = std::map<int, Model::Profile>{};
    auto activeProfileCount = gsl::narrow_cast<int>(_activeProfiles.Size());
    for (auto profileIndex = 0; profileIndex < activeProfileCount; profileIndex++)
    {
        remainingProfilesMap.emplace(profileIndex, _activeProfiles.GetAt(profileIndex));
    }

    // We keep track of the "remaining profiles" - those that have not yet been resolved
    // in either a "profile" or "source" entry. They will possibly be assigned to a
    // "remainingProfiles" entry
    auto remainingProfiles = single_threaded_map(std::move(remainingProfilesMap));

    // We call a recursive helper function to process the entries
    auto entries = _globals->NewTabMenu();
    _resolveNewTabMenuProfilesSet(entries, remainingProfiles, remainingProfilesEntry);

    // If a "remainingProfiles" entry has been found, assign to it the remaining profiles