Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
zadjii-msft committed Jun 2, 2024
1 parent d41e973 commit 091797c
Showing 1 changed file with 30 additions and 15 deletions.
45 changes: 30 additions & 15 deletions src/cascadia/TerminalSettingsModel/ActionMap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -824,6 +824,7 @@ namespace winrt::Microsoft::Terminal::Settings::Model::implementation
return _ExpandedCommandsCache;
}

#pragma region Snippets
std::vector<Model::Command> _filterToSnippets(IMapView<hstring, Model::Command> nameMap,
winrt::hstring currentCommandline,
const std::vector<Model::Command>& localCommands)
Expand Down Expand Up @@ -852,6 +853,8 @@ namespace winrt::Microsoft::Terminal::Settings::Model::implementation
return *copy;
};

// Helper to copy this command into a snippet-styled command, and any
// nested commands
const auto addCommand = [&](auto& command)
{
// If this is not a nested command, and it's a sendInput command...
Expand Down Expand Up @@ -889,18 +892,24 @@ namespace winrt::Microsoft::Terminal::Settings::Model::implementation
{
addCommand(command);
}

// ... and all the local commands passed in here
for (const auto& command : localCommands) {
addCommand(command);
}

// return winrt::single_threaded_vector<Model::Command>(std::move(results));
return results;
}

// Update ActionMap's cache of actions for this directory. We'll look for a
// .wt.json in this directory. If it exists, we'll read it, parse it's JSON,
// then take all the sendInput actions in it and store them in our
// _cwdLocalSnippetsCache
winrt::Windows::Foundation::IAsyncAction ActionMap::_updateLocalSnippetCache(winrt::hstring currentWorkingDirectory)
{
// Don't do I/O on the main thread, duh
co_await winrt::resume_background();

// This returns an empty string if we fail to load the file.
auto localTasksFileContents = CascadiaSettings::ReadFile(currentWorkingDirectory + L"\\.wt.json");
if (!localTasksFileContents.empty())
{
Expand All @@ -910,7 +919,12 @@ namespace winrt::Microsoft::Terminal::Settings::Model::implementation
Json::Value root;
if (!reader->parse(data.data(), data.data() + data.size(), &root, &errs))
{
throw winrt::hresult_error(WEB_E_INVALID_JSON_STRING, winrt::to_hstring(errs));
// In the real settings parser, we'd throw here:
// throw winrt::hresult_error(WEB_E_INVALID_JSON_STRING, winrt::to_hstring(errs));
//
// That seems overly agressive for something that we don't
// really own. Instead, just bail out.
co_return;
}

auto result = std::vector<Model::Command>();
Expand All @@ -920,44 +934,45 @@ namespace winrt::Microsoft::Terminal::Settings::Model::implementation
for (const auto& json : actions)
{
auto parsed = Command::FromJson(json, warnings, OriginTag::Generated);
// Skip over things that aren't snippets
if (parsed->ActionAndArgs().Action() != ShortcutAction::SendInput)
{
continue;
// commands.Append(*parsed);
}

result.push_back(*parsed);
}
}

_cwdLocalSnippetsCache.insert_or_assign(currentWorkingDirectory, result);
}

// Now at the bottom, we've either found a file successfully parsed it,
// and updated the _cwdLocalSnippetsCache. Or we failed at some point,
// and then it doesn't really matter.
co_return;
}

winrt::Windows::Foundation::IAsyncOperation<IVector<Model::Command>> ActionMap::FilterToSnippets(
winrt::hstring currentCommandline,
winrt::hstring currentWorkingDirectory)
{

// Check if there are any cached commands in this directory.
// If there aren't, then we'll try to look for any commands in this
// dir's .wt.json
auto cachedCwdCommands = _cwdLocalSnippetsCache.find(currentWorkingDirectory);
if (cachedCwdCommands == _cwdLocalSnippetsCache.end())
{
// we haven't cached this path yet
// Here, we haven't cached this path yet
co_await _updateLocalSnippetCache(currentWorkingDirectory);
cachedCwdCommands = _cwdLocalSnippetsCache.find(currentWorkingDirectory);
}

auto cachedCommands = cachedCwdCommands != _cwdLocalSnippetsCache.end() ?
cachedCwdCommands->second :
std::vector<Model::Command>{};

// if (cachedCwdCommands != _cwdLocalSnippetsCache.end())
// {
// const auto commands = cachedCwdCommands->second;
// for (const auto& cmd : commands)
// {
// results.Append(cmd);
// }
// }

co_return winrt::single_threaded_vector<Model::Command>(_filterToSnippets(NameMap(), currentCommandline, cachedCommands));
}
#pragma endregion
}

0 comments on commit 091797c

Please sign in to comment.