Improve clarity around _userProfileCount

This commit is contained in:
Leonard Hecker 2021-09-17 21:39:16 +02:00
parent 57c8991e9a
commit 7bcbd98246
2 changed files with 17 additions and 7 deletions

View file

@ -68,15 +68,13 @@ namespace winrt::Microsoft::Terminal::Settings::Model::implementation
static Json::Value _parseJSON(const std::string_view& content);
static const Json::Value& _getJSONValue(const Json::Value& json, const std::string_view& key) noexcept;
static bool _isValidProfileObject(const Json::Value& profileJson);
gsl::span<const winrt::com_ptr<implementation::Profile>> _getNonUserOriginProfiles() const;
void _parse(const OriginTag origin, const winrt::hstring& source, const std::string_view& content, ParsedSettings& settings);
void _appendProfile(winrt::com_ptr<implementation::Profile>&& profile, ParsedSettings& settings);
void _executeGenerator(const IDynamicProfileGenerator& generator);
std::unordered_set<std::wstring_view> _ignoredNamespaces;
// We treat userSettings.profiles as an append-only array and will
// append profiles into the userSettings as necessary in this function.
// We can thus get the gsl::span of user-given profiles, by preserving the size here
// and restoring it with gsl::make_span(userSettings.profiles).subspan(_userProfileCount).
// See _getNonUserOriginProfiles().
size_t _userProfileCount = 0;
};

View file

@ -1,4 +1,4 @@
// Copyright (c) Microsoft Corporation.
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
#include "pch.h"
@ -326,8 +326,7 @@ bool SettingsLoader::DisableDeletedProfiles()
auto generatedProfileIds = state->GeneratedProfiles();
bool newGeneratedProfiles = false;
// See member description of _userProfileCount for an explanation of this gsl::span trickery.
for (const auto& profile : gsl::make_span(userSettings.profiles).subspan(_userProfileCount))
for (const auto& profile : _getNonUserOriginProfiles())
{
if (generatedProfileIds.emplace(profile->Guid()).second)
{
@ -441,6 +440,19 @@ bool SettingsLoader::_isValidProfileObject(const Json::Value& profileJson)
profileJson.isMember(GuidKey.data(), GuidKey.data() + GuidKey.size())); // or has a guid
}
// We treat userSettings.profiles as an append-only array and will
// append profiles into the userSettings as necessary in this function.
// _userProfileCount stores the number of profiles that were in userJSON during construction.
//
// Thus no matter how many profiles are added later on, the following condition holds true:
// The userSettings.profiles in the range [0, _userProfileCount) contain all profiles specified by the user.
// In turn all profiles in the range [_userProfileCount, ∞) contain newly generated/added profiles.
// gsl::make_span(userSettings.profiles).subspan(_userProfileCount) gets us the latter range.
gsl::span<const winrt::com_ptr<Profile>> SettingsLoader::_getNonUserOriginProfiles() const
{
return gsl::make_span(userSettings.profiles).subspan(_userProfileCount);
}
// Parses the given JSON string ("content") and fills a ParsedSettings instance with it.
void SettingsLoader::_parse(const OriginTag origin, const winrt::hstring& source, const std::string_view& content, ParsedSettings& settings)
{