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

Code before:
            wil::unique_hfile hFile{ CreateFileW(protoExt.path().c_str(),
                                                 GENERIC_READ,
                                                 FILE_SHARE_READ | FILE_SHARE_WRITE,
                                                 nullptr,
                                                 OPEN_EXISTING,
                                                 FILE_ATTRIBUTE_NORMAL,
                                                 nullptr) };
            if (!hFile)
            {
                THROW_LAST_ERROR();
            }
            else
            {
                const auto fileData = _ReadFile(hFile.get()).value();
                jsonFiles.emplace(fileData);
            }
        }
    }
    return jsonFiles;
}

// Method Description:
// - Given a set of json files, uses them to modify existing profiles,
//   create new profiles, and create new color schemes
// Arguments:
// - stubs: the set of json files
// - source: the location the files came from
void CascadiaSettings::_AddOrModifyProfiles(const std::unordered_set<std::string> files, const winrt::hstring source)
{
    for (const auto& file : files)
    {
        // A file could have many new profiles/many profiles it wants to modify/many new color schemes
        // so we first parse the entire file into one json object
        Json::Value fullFile;

        _ParseJsonInto(file.data(), fullFile);

        if (fullFile.isMember(JsonKey(ProfilesKey)))
        {
            // Now we separately get each stub that modifies/adds a profile
            for (auto& profileStub : fullFile[JsonKey(ProfilesKey)])
            {
                if (profileStub.isMember(JsonKey(UpdatesKey)))
                {
                    // This stub is meant to be a modification to an existing profile,
                    // try to find the matching profile
                    profileStub[JsonKey(GuidKey)] = profileStub[JsonKey(UpdatesKey)];
                    auto matchingProfile = _FindMatchingProfile(profileStub);
                    if (matchingProfile)
                    {
                        // We found a matching profile, so layer the modification on top of it

Review comment (suggestion):
```suggestion
// - files: the set of json files
```

Code after:
// - A set containing all the found file data
std::unordered_set<std::string> CascadiaSettings::_AccumulateJsonFilesInDirectory(const std::wstring_view directory)
{
    std::unordered_set<std::string> jsonFiles;

    for (const auto& fragmentExt : std::filesystem::directory_iterator(directory))
    {
        if (fragmentExt.path().extension() == jsonExtension)
        {
            wil::unique_hfile hFile{ CreateFileW(fragmentExt.path().c_str(),
                                                 GENERIC_READ,
                                                 FILE_SHARE_READ | FILE_SHARE_WRITE,
                                                 nullptr,
                                                 OPEN_EXISTING,
                                                 FILE_ATTRIBUTE_NORMAL,
                                                 nullptr) };

            if (!hFile)
            {
                LOG_LAST_ERROR();
            }
            else
            {
                const auto fileData = _ReadFile(hFile.get()).value();
                jsonFiles.emplace(fileData);
            }
        }
    }
    return jsonFiles;
}

// Method Description:
// - Given a set of json files, uses them to modify existing profiles,
//   create new profiles, and create new color schemes
// Arguments:
// - files: the set of json files (each item in the set is the file data)
// - source: the location the files came from
void CascadiaSettings::_ParseAndLayerFragmentFiles(const std::unordered_set<std::string> files, const winrt::hstring source)
{
    for (const auto& file : files)
    {
        // A file could have many new profiles/many profiles it wants to modify/many new color schemes
        // so we first parse the entire file into one json object
        auto fullFile = _ParseUtf8JsonString(file.data());

        if (fullFile.isMember(JsonKey(ProfilesKey)))
        {
            // Now we separately get each stub that modifies/adds a profile
            // We intentionally don't use a const reference here because we modify
            // the profile stub by giving it a guid so we can call _FindMatchingProfile
            for (auto& profileStub : fullFile[JsonKey(ProfilesKey)])