Skip to content

Commit

Permalink
use a cache in actionmap to stash commands as we parse them
Browse files Browse the repository at this point in the history
  • Loading branch information
zadjii-msft committed Jun 2, 2024
1 parent 5466965 commit bba8aac
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 37 deletions.
2 changes: 1 addition & 1 deletion .wt.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
[
{
"command": { "action": "sendInput", "input": "bx\r" },
"name": "Build project",
"name": "Build projectttttttttttttttttt",
"description": "Build the project in the CWD"
},
{
Expand Down
56 changes: 29 additions & 27 deletions src/cascadia/TerminalApp/AppActionHandlers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1343,24 +1343,26 @@ namespace winrt::TerminalApp::implementation
std::vector<Command> commandsCollection;
Control::CommandHistoryContext context{ nullptr };
winrt::hstring currentCommandline = L"";
winrt::hstring currentWorkingDirectory = L"";

// If the user wanted to use the current commandline to filter results,
// OR they wanted command history (or some other source that
// requires context from the control)
// then get that here.
const bool shouldGetContext = realArgs.UseCommandline() ||
WI_IsAnyFlagSet(source, SuggestionsSource::CommandHistory | SuggestionsSource::Local);
if (shouldGetContext)
{
// const bool shouldGetContext = realArgs.UseCommandline() ||
// WI_IsAnyFlagSet(source, SuggestionsSource::CommandHistory | SuggestionsSource::Local);
// if (shouldGetContext)
// {
if (const auto& control{ _GetActiveControl() })
{
context = control.CommandHistory();
if (context)
{
currentCommandline = context.CurrentCommandline();
currentWorkingDirectory = context.CurrentWorkingDirectory();
}
}
}
// }

// Aggregate all the commands from the different sources that
// the user selected.
Expand All @@ -1369,7 +1371,7 @@ namespace winrt::TerminalApp::implementation
// their settings file. Ask the ActionMap for those.
if (WI_IsFlagSet(source, SuggestionsSource::Tasks))
{
const auto tasks = _settings.GlobalSettings().ActionMap().FilterToSendInput(currentCommandline);
const auto tasks = _settings.GlobalSettings().ActionMap().FilterToSnippets(currentCommandline, currentWorkingDirectory);
for (const auto& t : tasks)
{
commandsCollection.push_back(t);
Expand All @@ -1389,27 +1391,27 @@ namespace winrt::TerminalApp::implementation
}
}

if (WI_IsFlagSet(source, SuggestionsSource::Local) &&
context != nullptr)
{
// TODO! this is wack. CurrentWorkingDirectory should be it's own
// property, or a property of ControlCore.DirectoryHistory() or
// something. I only have 5 minutes to pch tho so garbage will do
auto cwd = context.CurrentWorkingDirectory();// strongControl.CommandHistory().CurrentWorkingDirectory();
if (!cwd.empty())
{
co_await winrt::resume_background();
auto localTasksFileContents = CascadiaSettings::ReadFile(cwd + L"\\.wt.json");
if (!localTasksFileContents.empty())
{
const auto localCommands = Command::ParseLocalCommands(localTasksFileContents);
for (const auto& t : localCommands)
{
commandsCollection.push_back(t);
}
}
}
}
// if (WI_IsFlagSet(source, SuggestionsSource::Local) &&
// context != nullptr)
// {
// // TODO! this is wack. CurrentWorkingDirectory should be it's own
// // property, or a property of ControlCore.DirectoryHistory() or
// // something. I only have 5 minutes to pch tho so garbage will do
// auto cwd = context.CurrentWorkingDirectory();// strongControl.CommandHistory().CurrentWorkingDirectory();
// if (!cwd.empty())
// {
// co_await winrt::resume_background();
// auto localTasksFileContents = CascadiaSettings::ReadFile(cwd + L"\\.wt.json");
// if (!localTasksFileContents.empty())
// {
// const auto localCommands = Command::ParseLocalCommands(localTasksFileContents);
// for (const auto& t : localCommands)
// {
// commandsCollection.push_back(t);
// }
// }
// }
// }

co_await wil::resume_foreground(Dispatcher());

Expand Down
67 changes: 61 additions & 6 deletions src/cascadia/TerminalSettingsModel/ActionMap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -824,8 +824,9 @@ namespace winrt::Microsoft::Terminal::Settings::Model::implementation
return _ExpandedCommandsCache;
}

IVector<Model::Command> _filterToSendInput(IMapView<hstring, Model::Command> nameMap,
winrt::hstring currentCommandline)
IVector<Model::Command> _filterToSnippets(IMapView<hstring, Model::Command> nameMap,
winrt::hstring currentCommandline,
winrt::hstring currentWorkingDirectory)
{
auto results = winrt::single_threaded_vector<Model::Command>();

Expand Down Expand Up @@ -865,7 +866,7 @@ namespace winrt::Microsoft::Terminal::Settings::Model::implementation
else if (command.HasNestedCommands())
{
// Look for any sendInput commands nested underneath us
auto innerResults = _filterToSendInput(command.NestedCommands(), currentCommandline);
auto innerResults = _filterToSnippets(command.NestedCommands(), currentCommandline, currentWorkingDirectory);

if (innerResults.Size() > 0)
{
Expand All @@ -886,9 +887,63 @@ namespace winrt::Microsoft::Terminal::Settings::Model::implementation
return results;
}

IVector<Model::Command> ActionMap::FilterToSendInput(
winrt::hstring currentCommandline)
IVector<Model::Command> ActionMap::FilterToSnippets(
winrt::hstring currentCommandline,
winrt::hstring currentWorkingDirectory)
{
return _filterToSendInput(NameMap(), currentCommandline);
auto results = _filterToSnippets(NameMap(), currentCommandline, currentWorkingDirectory);

auto cachedCwdCommands = _cwdLocalSnippetsCache.find(currentWorkingDirectory);
if (cachedCwdCommands == _cwdLocalSnippetsCache.end())
{
// we haven't cached this path yet

auto localTasksFileContents = CascadiaSettings::ReadFile(currentWorkingDirectory + L"\\.wt.json");
if (!localTasksFileContents.empty())
{
// const auto localCommands = Command::ParseLocalCommands(localTasksFileContents);
// for (const auto& t : localCommands)
// {
// commandsCollection.push_back(t);
// }

auto data = winrt::to_string(localTasksFileContents);
std::string errs;
static std::unique_ptr<Json::CharReader> reader{ Json::CharReaderBuilder::CharReaderBuilder().newCharReader() };
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));
}

auto result = std::vector<Model::Command>();
if (auto actions{ root[JsonKey("actions")] })
{
std::vector<SettingsLoadWarnings> warnings;
for (const auto& json : actions)
{
auto parsed = Command::FromJson(json, warnings, OriginTag::Generated);
if (parsed->ActionAndArgs().Action() != ShortcutAction::SendInput)
continue;
// commands.Append(*parsed);
result.push_back(*parsed);
}
}

_cwdLocalSnippetsCache.insert_or_assign(currentWorkingDirectory, result);
cachedCwdCommands = _cwdLocalSnippetsCache.find(currentWorkingDirectory);
}
}

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

return results;
}
}
4 changes: 3 additions & 1 deletion src/cascadia/TerminalSettingsModel/ActionMap.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ namespace winrt::Microsoft::Terminal::Settings::Model::implementation
void ExpandCommands(const Windows::Foundation::Collections::IVectorView<Model::Profile>& profiles,
const Windows::Foundation::Collections::IMapView<winrt::hstring, Model::ColorScheme>& schemes);

winrt::Windows::Foundation::Collections::IVector<Model::Command> FilterToSendInput(winrt::hstring currentCommandline);
winrt::Windows::Foundation::Collections::IVector<Model::Command> FilterToSnippets(winrt::hstring currentCommandline, winrt::hstring currentWorkingDirectory);

private:
Model::Command _GetActionByID(const winrt::hstring actionID) const;
Expand Down Expand Up @@ -128,6 +128,8 @@ namespace winrt::Microsoft::Terminal::Settings::Model::implementation
// we can give the SUI a view of the key chords and the commands they map to
Windows::Foundation::Collections::IMap<Control::KeyChord, Model::Command> _ResolvedKeyActionMapCache{ nullptr };

std::unordered_map<hstring, std::vector<Model::Command>> _cwdLocalSnippetsCache{};

friend class SettingsModelUnitTests::KeyBindingsTests;
friend class SettingsModelUnitTests::DeserializationTests;
friend class SettingsModelUnitTests::TerminalSettingsTests;
Expand Down
2 changes: 1 addition & 1 deletion src/cascadia/TerminalSettingsModel/ActionMap.idl
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ namespace Microsoft.Terminal.Settings.Model

IVector<Command> ExpandedCommands { get; };

IVector<Command> FilterToSendInput(String CurrentCommandline);
IVector<Command> FilterToSnippets(String CurrentCommandline, String CurrentWorkingDirectory);
};

[default_interface] runtimeclass ActionMap : IActionMapView
Expand Down
2 changes: 1 addition & 1 deletion src/cascadia/TerminalSettingsModel/Command.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -724,7 +724,7 @@ namespace winrt::Microsoft::Terminal::Settings::Model::implementation
std::vector<SettingsLoadWarnings> warnings;
for (const auto& json : actions)
{
auto parsed = Command::FromJson(json, warnings, OriginTag::Generated, false);
auto parsed = Command::FromJson(json, warnings, OriginTag::Generated);
if (parsed->ActionAndArgs().Action() != ShortcutAction::SendInput)
continue;
// commands.Append(*parsed);
Expand Down

0 comments on commit bba8aac

Please sign in to comment.