Skip to content

Commit

Permalink
Actually works in suggestions UI again
Browse files Browse the repository at this point in the history
  • Loading branch information
zadjii-msft committed Jun 2, 2024
1 parent b5d063b commit 5466965
Show file tree
Hide file tree
Showing 8 changed files with 98 additions and 55 deletions.
2 changes: 2 additions & 0 deletions .wt.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@
{
"command": { "action": "sendInput", "input": "bcz\r" },
"name": "Clean & build solution",
"icon": "\uE8e6",
"description": "Start over. Go get your coffee. "
},
{
"command": { "action": "sendInput", "input": "nuget push -apikey az -source TerminalDependencies %userprofile%\\Downloads" },
"name": "Upload package to nuget feed",
"icon": "\uE898",
"description": "Go download a .nupkg, put it in ~/Downloads, and use this to push to our private feed."
},

Expand Down
129 changes: 80 additions & 49 deletions src/cascadia/TerminalApp/AppActionHandlers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1330,64 +1330,95 @@ namespace winrt::TerminalApp::implementation
{
if (const auto& realArgs = args.ActionArgs().try_as<SuggestionsArgs>())
{
const auto source = realArgs.Source();
std::vector<Command> commandsCollection;
Control::CommandHistoryContext context{ nullptr };
winrt::hstring currentCommandline = 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_IsFlagSet(source, SuggestionsSource::CommandHistory);
if (shouldGetContext)
{
if (const auto& control{ _GetActiveControl() })
{
context = control.CommandHistory();
if (context)
{
currentCommandline = context.CurrentCommandline();
}
}
}
_doHandleSuggestions(realArgs);

args.Handled(true);
}
}
}

// Aggregate all the commands from the different sources that
// the user selected.
winrt::fire_and_forget TerminalPage::_doHandleSuggestions(SuggestionsArgs realArgs)
{
const auto source = realArgs.Source();
std::vector<Command> commandsCollection;
Control::CommandHistoryContext context{ nullptr };
winrt::hstring currentCommandline = L"";

// Tasks are all the sendInput commands the user has saved in
// their settings file. Ask the ActionMap for those.
if (WI_IsFlagSet(source, SuggestionsSource::Tasks))
// 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)
{
if (const auto& control{ _GetActiveControl() })
{
context = control.CommandHistory();
if (context)
{
const auto tasks = _settings.GlobalSettings().ActionMap().FilterToSendInput(currentCommandline);
for (const auto& t : tasks)
{
commandsCollection.push_back(t);
}
currentCommandline = context.CurrentCommandline();
}
}
}

// Aggregate all the commands from the different sources that
// the user selected.

// Command History comes from the commands in the buffer,
// assuming the user has enabled shell integration. Get those
// from the active control.
if (WI_IsFlagSet(source, SuggestionsSource::CommandHistory) &&
context != nullptr)
// Tasks are all the sendInput commands the user has saved in
// their settings file. Ask the ActionMap for those.
if (WI_IsFlagSet(source, SuggestionsSource::Tasks))
{
const auto tasks = _settings.GlobalSettings().ActionMap().FilterToSendInput(currentCommandline);
for (const auto& t : tasks)
{
commandsCollection.push_back(t);
}
}

// Command History comes from the commands in the buffer,
// assuming the user has enabled shell integration. Get those
// from the active control.
if (WI_IsFlagSet(source, SuggestionsSource::CommandHistory) &&
context != nullptr)
{
const auto recentCommands = Command::HistoryToCommands(context.History(), currentCommandline, false);
for (const auto& t : recentCommands)
{
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 recentCommands = Command::HistoryToCommands(context.History(), currentCommandline, false);
for (const auto& t : recentCommands)
{
commandsCollection.push_back(t);
}
const auto localCommands = Command::ParseLocalCommands(localTasksFileContents);
for (const auto& t : localCommands)
{
commandsCollection.push_back(t);
}
}

// Open the palette with all these commands in it.
_OpenSuggestions(_GetActiveControl(),
winrt::single_threaded_vector<Command>(std::move(commandsCollection)),
SuggestionsMode::Palette,
currentCommandline);
args.Handled(true);
}
}

co_await wil::resume_foreground(Dispatcher());

// Open the palette with all these commands in it.
_OpenSuggestions(_GetActiveControl(),
winrt::single_threaded_vector<Command>(std::move(commandsCollection)),
SuggestionsMode::Palette,
currentCommandline);

}

void TerminalPage::_HandleColorSelection(const IInspectable& /*sender*/,
Expand Down
3 changes: 3 additions & 0 deletions src/cascadia/TerminalApp/TerminalPage.h
Original file line number Diff line number Diff line change
Expand Up @@ -543,6 +543,9 @@ namespace winrt::TerminalApp::implementation
winrt::Microsoft::Terminal::Control::TermControl _senderOrActiveControl(const winrt::Windows::Foundation::IInspectable& sender);
winrt::com_ptr<TerminalTab> _senderOrFocusedTab(const IInspectable& sender);

void _activePaneChanged(winrt::TerminalApp::TerminalTab tab, Windows::Foundation::IInspectable args);
winrt::fire_and_forget _doHandleSuggestions(Microsoft::Terminal::Settings::Model::SuggestionsArgs realArgs);

#pragma region ActionHandlers
// These are all defined in AppActionHandlers.cpp
#define ON_ALL_ACTIONS(action) DECLARE_ACTION_HANDLER(action);
Expand Down
1 change: 1 addition & 0 deletions src/cascadia/TerminalSettingsModel/ActionArgs.idl
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ namespace Microsoft.Terminal.Settings.Model
Tasks = 0x1,
CommandHistory = 0x2,
DirectoryHistory = 0x4,
Local = 0x8,
All = 0xffffffff,
};

Expand Down
10 changes: 7 additions & 3 deletions src/cascadia/TerminalSettingsModel/Command.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -707,8 +707,7 @@ namespace winrt::Microsoft::Terminal::Settings::Model::implementation
return winrt::single_threaded_vector<Model::Command>(std::move(result));
}

void Command::AddLocalCommands(const Windows::Foundation::Collections::IVector<Model::Command>& commands,
winrt::hstring localTasksFileContents)
IVector<Model::Command> Command::ParseLocalCommands(winrt::hstring localTasksFileContents)
{
auto data = winrt::to_string(localTasksFileContents);
std::string errs;
Expand All @@ -718,6 +717,8 @@ namespace winrt::Microsoft::Terminal::Settings::Model::implementation
{
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;
Expand All @@ -726,9 +727,12 @@ namespace winrt::Microsoft::Terminal::Settings::Model::implementation
auto parsed = Command::FromJson(json, warnings, OriginTag::Generated, false);
if (parsed->ActionAndArgs().Action() != ShortcutAction::SendInput)
continue;
commands.Append(*parsed);
// commands.Append(*parsed);
result.push_back(*parsed);
}
}

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

}
2 changes: 1 addition & 1 deletion src/cascadia/TerminalSettingsModel/Command.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ namespace winrt::Microsoft::Terminal::Settings::Model::implementation
static Windows::Foundation::Collections::IVector<Model::Command> HistoryToCommands(Windows::Foundation::Collections::IVector<winrt::hstring> history,
winrt::hstring currentCommandline,
bool directories);
static void AddLocalCommands(const Windows::Foundation::Collections::IVector<Model::Command>&, winrt::hstring localTasksFileContents);
static Windows::Foundation::Collections::IVector<Model::Command> ParseLocalCommands(winrt::hstring localTasksFileContents);

WINRT_PROPERTY(ExpandCommandType, IterateOn, ExpandCommandType::None);
WINRT_PROPERTY(Model::ActionAndArgs, ActionAndArgs);
Expand Down
2 changes: 1 addition & 1 deletion src/cascadia/TerminalSettingsModel/Command.idl
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,6 @@ namespace Microsoft.Terminal.Settings.Model

static IVector<Command> ParsePowerShellMenuComplete(String json, Int32 replaceLength);
static IVector<Command> HistoryToCommands(IVector<String> commandHistory, String commandline, Boolean directories);
static void AddLocalCommands(IVector<Command> commands, String localTasksFileContents);
static IVector<Command> ParseLocalCommands(String localTasksFileContents);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -501,11 +501,13 @@ JSON_ENUM_MAPPER(::winrt::Microsoft::Terminal::Settings::Model::FindMatchDirecti

JSON_FLAG_MAPPER(::winrt::Microsoft::Terminal::Settings::Model::SuggestionsSource)
{
static constexpr std::array<pair_type, 5> mappings = {
static constexpr std::array<pair_type, 7> mappings = {
pair_type{ "none", AllClear },
pair_type{ "tasks", ValueType::Tasks },
pair_type{ "snippets", ValueType::Tasks },
pair_type{ "commandHistory", ValueType::CommandHistory },
pair_type{ "directoryHistory", ValueType::DirectoryHistory },
pair_type{ "local", ValueType::Local },
pair_type{ "all", AllSet },
};
};
Expand Down

0 comments on commit 5466965

Please sign in to comment.