mirror of
https://github.com/microsoft/terminal.git
synced 2026-05-21 14:27:22 +00:00
clean up this duplicated code
This commit is contained in:
@@ -731,39 +731,9 @@ std::string CascadiaSettings::_ApplyFirstRunChangesToSettingsTemplate(std::strin
|
||||
// - <none>
|
||||
void CascadiaSettings::_ExpandCommands()
|
||||
{
|
||||
std::vector<winrt::hstring> commandsToRemove;
|
||||
std::vector<winrt::TerminalApp::Command> commandsToAdd;
|
||||
|
||||
// Any commands that return a non-empty vector from ExpandCommand should be
|
||||
// replaced with all the commands that ExpandCommand returned. Since we
|
||||
// can't modify the map while we're iterating over it, we'll do this in a
|
||||
// couple steps.
|
||||
|
||||
// First, collect up all the commands that need replacing.
|
||||
for (auto nameAndCmd : _globals.GetCommands())
|
||||
{
|
||||
winrt::com_ptr<winrt::TerminalApp::implementation::Command> cmd;
|
||||
cmd.copy_from(winrt::get_self<winrt::TerminalApp::implementation::Command>(nameAndCmd.second));
|
||||
|
||||
auto newCommands = winrt::TerminalApp::implementation::Command::ExpandCommand(cmd, _profiles, _warnings);
|
||||
if (newCommands.size() > 0)
|
||||
{
|
||||
commandsToRemove.push_back(nameAndCmd.first);
|
||||
commandsToAdd.insert(commandsToAdd.end(), newCommands.begin(), newCommands.end());
|
||||
}
|
||||
}
|
||||
|
||||
// Second, remove all the commands that need to be removed.
|
||||
for (auto& name : commandsToRemove)
|
||||
{
|
||||
_globals.GetCommands().erase(name);
|
||||
}
|
||||
|
||||
// Finally, add all the new commands.
|
||||
for (auto& cmd : commandsToAdd)
|
||||
{
|
||||
_globals.GetCommands().insert_or_assign(cmd.Name(), cmd);
|
||||
}
|
||||
winrt::TerminalApp::implementation::Command::ExpandCommands(_globals.GetCommands(),
|
||||
_profiles,
|
||||
_warnings);
|
||||
}
|
||||
|
||||
// Method Description:
|
||||
|
||||
@@ -314,6 +314,53 @@ namespace winrt::TerminalApp::implementation
|
||||
return winrt::hstring{ result };
|
||||
}
|
||||
|
||||
// Method Description:
|
||||
// - Iterate over all the provided comamnds, and recursively expand any
|
||||
// commands with `iterateOn` set. If we successfully generated expanded
|
||||
// commands for them, then we'll remove the original command, and add all
|
||||
// the newly generated commands.
|
||||
// - For more specific implementation details, see _expandCommand.
|
||||
// Arguments:
|
||||
// - commands: a map of commands to expand. Newly created commands will be
|
||||
// inserted into the map to replace the expandable commands.
|
||||
// - profiles: A list of all the profiles that this command should be expanded on.
|
||||
// - warnings: If there were any warnings during parsing, they'll be
|
||||
// appended to this vector.
|
||||
// Return Value:
|
||||
// - <none>
|
||||
void Command::ExpandCommands(std::unordered_map<winrt::hstring, winrt::TerminalApp::Command>& commands,
|
||||
const std::vector<::TerminalApp::Profile>& profiles,
|
||||
std::vector<::TerminalApp::SettingsLoadWarnings>& warnings)
|
||||
{
|
||||
std::vector<winrt::hstring> commandsToRemove;
|
||||
std::vector<winrt::TerminalApp::Command> commandsToAdd;
|
||||
// First, collect up all the commands that need replacing.
|
||||
for (auto nameAndCmd : commands)
|
||||
{
|
||||
winrt::com_ptr<winrt::TerminalApp::implementation::Command> cmd;
|
||||
cmd.copy_from(winrt::get_self<winrt::TerminalApp::implementation::Command>(nameAndCmd.second));
|
||||
|
||||
auto newCommands = _expandCommand(cmd, profiles, warnings);
|
||||
if (newCommands.size() > 0)
|
||||
{
|
||||
commandsToRemove.push_back(nameAndCmd.first);
|
||||
commandsToAdd.insert(commandsToAdd.end(), newCommands.begin(), newCommands.end());
|
||||
}
|
||||
}
|
||||
|
||||
// Second, remove all the commands that need to be removed.
|
||||
for (auto& name : commandsToRemove)
|
||||
{
|
||||
commands.erase(name);
|
||||
}
|
||||
|
||||
// Finally, add all the new commands.
|
||||
for (auto& cmd : commandsToAdd)
|
||||
{
|
||||
commands.insert_or_assign(cmd.Name(), cmd);
|
||||
}
|
||||
}
|
||||
|
||||
// Function Description:
|
||||
// - Attempts to expand the given command into many commands, if the command
|
||||
// has `"iterateOn": "profiles"` set.
|
||||
@@ -336,46 +383,16 @@ namespace winrt::TerminalApp::implementation
|
||||
// Return Value:
|
||||
// - and empty vector if the command wasn't expandable, otherwise a list of
|
||||
// the newly-created commands.
|
||||
std::vector<winrt::TerminalApp::Command> Command::ExpandCommand(winrt::com_ptr<Command> expandable,
|
||||
const std::vector<::TerminalApp::Profile>& profiles,
|
||||
std::vector<::TerminalApp::SettingsLoadWarnings>& warnings)
|
||||
std::vector<winrt::TerminalApp::Command> Command::_expandCommand(winrt::com_ptr<Command> expandable,
|
||||
const std::vector<::TerminalApp::Profile>& profiles,
|
||||
std::vector<::TerminalApp::SettingsLoadWarnings>& warnings)
|
||||
{
|
||||
std::vector<winrt::TerminalApp::Command> newCommands;
|
||||
|
||||
////////////////////////////////////////////////////////////////////////
|
||||
if (!expandable->_subcommands.empty())
|
||||
{
|
||||
// Blatantly copied from CascadiaSettings::_ExpandCommands
|
||||
|
||||
std::vector<winrt::hstring> commandsToRemove;
|
||||
std::vector<winrt::TerminalApp::Command> commandsToAdd;
|
||||
// First, collect up all the commands that need replacing.
|
||||
for (auto nameAndCmd : expandable->_subcommands)
|
||||
{
|
||||
winrt::com_ptr<winrt::TerminalApp::implementation::Command> cmd;
|
||||
cmd.copy_from(winrt::get_self<winrt::TerminalApp::implementation::Command>(nameAndCmd.second));
|
||||
|
||||
auto newCommands = winrt::TerminalApp::implementation::Command::ExpandCommand(cmd, profiles, warnings);
|
||||
if (newCommands.size() > 0)
|
||||
{
|
||||
commandsToRemove.push_back(nameAndCmd.first);
|
||||
commandsToAdd.insert(commandsToAdd.end(), newCommands.begin(), newCommands.end());
|
||||
}
|
||||
}
|
||||
|
||||
// Second, remove all the commands that need to be removed.
|
||||
for (auto& name : commandsToRemove)
|
||||
{
|
||||
expandable->_subcommands.erase(name);
|
||||
}
|
||||
|
||||
// Finally, add all the new commands.
|
||||
for (auto& cmd : commandsToAdd)
|
||||
{
|
||||
expandable->_subcommands.insert_or_assign(cmd.Name(), cmd);
|
||||
}
|
||||
ExpandCommands(expandable->_subcommands, profiles, warnings);
|
||||
}
|
||||
////////////////////////////////////////////////////////////////////////
|
||||
|
||||
if (expandable->_IterateOn == ExpandCommandType::None)
|
||||
{
|
||||
|
||||
@@ -46,9 +46,9 @@ namespace winrt::TerminalApp::implementation
|
||||
std::vector<::TerminalApp::SettingsLoadWarnings>& warnings,
|
||||
const bool postExpansion = false);
|
||||
|
||||
static std::vector<winrt::TerminalApp::Command> ExpandCommand(winrt::com_ptr<Command> expandable,
|
||||
const std::vector<::TerminalApp::Profile>& profiles,
|
||||
std::vector<::TerminalApp::SettingsLoadWarnings>& warnings);
|
||||
static void ExpandCommands(std::unordered_map<winrt::hstring, winrt::TerminalApp::Command>& commands,
|
||||
const std::vector<::TerminalApp::Profile>& profiles,
|
||||
std::vector<::TerminalApp::SettingsLoadWarnings>& warnings);
|
||||
|
||||
static std::vector<::TerminalApp::SettingsLoadWarnings> LayerJson(std::unordered_map<winrt::hstring, winrt::TerminalApp::Command>& commands,
|
||||
const Json::Value& json);
|
||||
@@ -66,6 +66,10 @@ namespace winrt::TerminalApp::implementation
|
||||
std::unordered_map<winrt::hstring, winrt::TerminalApp::Command> _subcommands;
|
||||
Windows::Foundation::Collections::IObservableVector<TerminalApp::Command> _nestedCommandsView{ nullptr };
|
||||
|
||||
static std::vector<winrt::TerminalApp::Command> _expandCommand(winrt::com_ptr<Command> expandable,
|
||||
const std::vector<::TerminalApp::Profile>& profiles,
|
||||
std::vector<::TerminalApp::SettingsLoadWarnings>& warnings);
|
||||
|
||||
friend class TerminalAppLocalTests::SettingsTests;
|
||||
friend class TerminalAppLocalTests::CommandTests;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user