Skip to content

Commit

Permalink
add origin tag
Browse files Browse the repository at this point in the history
  • Loading branch information
PankajBhojwani committed Mar 5, 2024
1 parent ad51b22 commit 90627b3
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 28 deletions.
3 changes: 1 addition & 2 deletions src/cascadia/TerminalSettingsModel/ActionMap.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,7 @@ namespace winrt::Microsoft::Terminal::Settings::Model::implementation
void AddAction(const Model::Command& cmd);

// JSON
static com_ptr<ActionMap> FromJson(const Json::Value& json);
std::vector<SettingsLoadWarnings> LayerJson(const Json::Value& json, const bool withKeybindings = true);
std::vector<SettingsLoadWarnings> LayerJson(const Json::Value& json, const OriginTag origin, const bool withKeybindings = true);
Json::Value ToJson() const;

// modification
Expand Down
11 changes: 2 additions & 9 deletions src/cascadia/TerminalSettingsModel/ActionMapSerialization.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,6 @@ using namespace winrt::Microsoft::Terminal::Settings::Model;

namespace winrt::Microsoft::Terminal::Settings::Model::implementation
{
com_ptr<ActionMap> ActionMap::FromJson(const Json::Value& json)
{
auto result = make_self<ActionMap>();
result->LayerJson(json);
return result;
}

// Method Description:
// - Deserialize an ActionMap from the array `json`. The json array should contain
// an array of serialized `Command` objects.
Expand All @@ -35,7 +28,7 @@ namespace winrt::Microsoft::Terminal::Settings::Model::implementation
// - json: an array of Json::Value's to deserialize into our ActionMap.
// Return value:
// - a list of warnings encountered while deserializing the json
std::vector<SettingsLoadWarnings> ActionMap::LayerJson(const Json::Value& json, const bool withKeybindings)
std::vector<SettingsLoadWarnings> ActionMap::LayerJson(const Json::Value& json, const OriginTag origin, const bool withKeybindings)
{
// It's possible that the user provided keybindings have some warnings in
// them - problems that we should alert the user to, but we can recover
Expand All @@ -50,7 +43,7 @@ namespace winrt::Microsoft::Terminal::Settings::Model::implementation
continue;
}

AddAction(*Command::FromJson(cmdJson, warnings, withKeybindings));
AddAction(*Command::FromJson(cmdJson, warnings, origin, withKeybindings));
}

return warnings;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,8 @@ void SettingsLoader::FinalizeLayering()
if (userSettings.globals->EnableColorSelection())
{
const auto json = _parseJson(EnableColorSelectionSettingsJson);
const auto globals = GlobalAppSettings::FromJson(json.root);
// todo: figure out the correct origin tag here
const auto globals = GlobalAppSettings::FromJson(json.root, OriginTag::None);
userSettings.globals->AddLeastImportantParent(globals);
}

Expand Down Expand Up @@ -616,7 +617,7 @@ void SettingsLoader::_parse(const OriginTag origin, const winrt::hstring& source
settings.clear();

{
settings.globals = GlobalAppSettings::FromJson(json.root);
settings.globals = GlobalAppSettings::FromJson(json.root, origin);

for (const auto& schemeJson : json.colorSchemes)
{
Expand Down Expand Up @@ -704,7 +705,7 @@ void SettingsLoader::_parseFragment(const winrt::hstring& source, const std::str
// Parse out actions from the fragment. Manually opt-out of keybinding
// parsing - fragments shouldn't be allowed to bind actions to keys
// directly. We may want to revisit circa GH#2205
settings.globals->LayerActionsFrom(json.root, false);
settings.globals->LayerActionsFrom(json.root, OriginTag::Fragment, false);
}

{
Expand Down
11 changes: 7 additions & 4 deletions src/cascadia/TerminalSettingsModel/Command.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -259,9 +259,11 @@ namespace winrt::Microsoft::Terminal::Settings::Model::implementation
// - the newly constructed Command object.
winrt::com_ptr<Command> Command::FromJson(const Json::Value& json,
std::vector<SettingsLoadWarnings>& warnings,
const OriginTag origin,
const bool parseKeys)
{
auto result = winrt::make_self<Command>();
result->_Origin = origin;

auto nested = false;
JsonUtils::GetValueForKey(json, IterateOnKey, result->_IterateOn);
Expand All @@ -274,7 +276,7 @@ namespace winrt::Microsoft::Terminal::Settings::Model::implementation
// Initialize our list of subcommands.
result->_subcommands = winrt::single_threaded_map<winrt::hstring, Model::Command>();
result->_nestedCommand = true;
auto nestedWarnings = Command::LayerJson(result->_subcommands, nestedCommandsJson);
auto nestedWarnings = Command::LayerJson(result->_subcommands, nestedCommandsJson, origin);
// It's possible that the nested commands have some warnings
warnings.insert(warnings.end(), nestedWarnings.begin(), nestedWarnings.end());

Expand Down Expand Up @@ -362,7 +364,8 @@ namespace winrt::Microsoft::Terminal::Settings::Model::implementation
// Return Value:
// - A vector containing any warnings detected while parsing
std::vector<SettingsLoadWarnings> Command::LayerJson(IMap<winrt::hstring, Model::Command>& commands,
const Json::Value& json)
const Json::Value& json,
const OriginTag origin)
{
std::vector<SettingsLoadWarnings> warnings;

Expand All @@ -372,7 +375,7 @@ namespace winrt::Microsoft::Terminal::Settings::Model::implementation
{
try
{
const auto result = Command::FromJson(value, warnings);
const auto result = Command::FromJson(value, warnings, origin);
if (result->ActionAndArgs().Action() == ShortcutAction::Invalid && !result->HasNestedCommands())
{
// If there wasn't a parsed command, then try to get the
Expand Down Expand Up @@ -583,7 +586,7 @@ namespace winrt::Microsoft::Terminal::Settings::Model::implementation
// warnings, but ultimately, we don't care about warnings during
// expansion.
std::vector<SettingsLoadWarnings> unused;
if (auto newCmd{ Command::FromJson(newJsonValue, unused) })
if (auto newCmd{ Command::FromJson(newJsonValue, unused, expandable->_Origin) })
{
newCommands.push_back(*newCmd);
}
Expand Down
5 changes: 4 additions & 1 deletion src/cascadia/TerminalSettingsModel/Command.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,16 @@ namespace winrt::Microsoft::Terminal::Settings::Model::implementation

static winrt::com_ptr<Command> FromJson(const Json::Value& json,
std::vector<SettingsLoadWarnings>& warnings,
const OriginTag origin,
const bool parseKeys = true);

static void ExpandCommands(Windows::Foundation::Collections::IMap<winrt::hstring, Model::Command>& commands,
Windows::Foundation::Collections::IVectorView<Model::Profile> profiles,
Windows::Foundation::Collections::IVectorView<Model::ColorScheme> schemes);

static std::vector<SettingsLoadWarnings> LayerJson(Windows::Foundation::Collections::IMap<winrt::hstring, Model::Command>& commands,
const Json::Value& json);
const Json::Value& json,
const OriginTag origin);
Json::Value ToJson() const;

bool HasNestedCommands() const;
Expand Down Expand Up @@ -75,6 +77,7 @@ namespace winrt::Microsoft::Terminal::Settings::Model::implementation

WINRT_PROPERTY(ExpandCommandType, IterateOn, ExpandCommandType::None);
WINRT_PROPERTY(Model::ActionAndArgs, ActionAndArgs);
WINRT_PROPERTY(OriginTag, Origin);

private:
Json::Value _originalJson;
Expand Down
1 change: 1 addition & 0 deletions src/cascadia/TerminalSettingsModel/Command.idl
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ namespace Microsoft.Terminal.Settings.Model
Command();

String Name { get; };
OriginTag Origin;
ActionAndArgs ActionAndArgs { get; };
Microsoft.Terminal.Control.KeyChord Keys { get; };
void RegisterKey(Microsoft.Terminal.Control.KeyChord keys);
Expand Down
12 changes: 6 additions & 6 deletions src/cascadia/TerminalSettingsModel/GlobalAppSettings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,14 +117,14 @@ winrt::Microsoft::Terminal::Settings::Model::ActionMap GlobalAppSettings::Action
// - json: an object which should be a serialization of a GlobalAppSettings object.
// Return Value:
// - a new GlobalAppSettings instance created from the values in `json`
winrt::com_ptr<GlobalAppSettings> GlobalAppSettings::FromJson(const Json::Value& json)
winrt::com_ptr<GlobalAppSettings> GlobalAppSettings::FromJson(const Json::Value& json, const OriginTag origin)
{
auto result = winrt::make_self<GlobalAppSettings>();
result->LayerJson(json);
result->LayerJson(json, origin);
return result;
}

void GlobalAppSettings::LayerJson(const Json::Value& json)
void GlobalAppSettings::LayerJson(const Json::Value& json, const OriginTag origin)
{
JsonUtils::GetValueForKey(json, DefaultProfileKey, _UnparsedDefaultProfile);
// GH#8076 - when adding enum values to this key, we also changed it from
Expand All @@ -137,19 +137,19 @@ void GlobalAppSettings::LayerJson(const Json::Value& json)
MTSM_GLOBAL_SETTINGS(GLOBAL_SETTINGS_LAYER_JSON)
#undef GLOBAL_SETTINGS_LAYER_JSON

LayerActionsFrom(json, true);
LayerActionsFrom(json, origin, true);

JsonUtils::GetValueForKey(json, LegacyReloadEnvironmentVariablesKey, _legacyReloadEnvironmentVariables);
}

void GlobalAppSettings::LayerActionsFrom(const Json::Value& json, const bool withKeybindings)
void GlobalAppSettings::LayerActionsFrom(const Json::Value& json, const OriginTag origin, const bool withKeybindings)
{
static constexpr std::array bindingsKeys{ LegacyKeybindingsKey, ActionsKey };
for (const auto& jsonKey : bindingsKeys)
{
if (auto bindings{ json[JsonKey(jsonKey)] })
{
auto warnings = _actionMap->LayerJson(bindings, withKeybindings);
auto warnings = _actionMap->LayerJson(bindings, origin, withKeybindings);

// It's possible that the user provided keybindings have some warnings
// in them - problems that we should alert the user to, but we can
Expand Down
6 changes: 3 additions & 3 deletions src/cascadia/TerminalSettingsModel/GlobalAppSettings.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ namespace winrt::Microsoft::Terminal::Settings::Model::implementation

Model::ActionMap ActionMap() const noexcept;

static com_ptr<GlobalAppSettings> FromJson(const Json::Value& json);
void LayerJson(const Json::Value& json);
void LayerActionsFrom(const Json::Value& json, const bool withKeybindings = true);
static com_ptr<GlobalAppSettings> FromJson(const Json::Value& json, const OriginTag origin);
void LayerJson(const Json::Value& json, const OriginTag origin);
void LayerActionsFrom(const Json::Value& json, const OriginTag origin, const bool withKeybindings = true);

Json::Value ToJson() const;

Expand Down

0 comments on commit 90627b3

Please sign in to comment.