Skip to content

Commit 798dd09

Browse files
committed
Add scrollback source for suggestions
1 parent bdf4432 commit 798dd09

26 files changed

+325
-93
lines changed

src/cascadia/TerminalApp/AppActionHandlers.cpp

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1454,15 +1454,17 @@ namespace winrt::TerminalApp::implementation
14541454
const auto source = realArgs.Source();
14551455
std::vector<Command> commandsCollection;
14561456
Control::CommandHistoryContext context{ nullptr };
1457-
winrt::hstring currentCommandline;
14581457
winrt::hstring currentWorkingDirectory;
1458+
winrt::hstring filter;
1459+
1460+
bool sortResults = source == SuggestionsSource::Scrollback;
14591461

14601462
// If the user wanted to use the current commandline to filter results,
14611463
// OR they wanted command history (or some other source that
14621464
// requires context from the control)
14631465
// then get that here.
14641466
const bool shouldGetContext = realArgs.UseCommandline() ||
1465-
WI_IsAnyFlagSet(source, SuggestionsSource::CommandHistory | SuggestionsSource::QuickFixes);
1467+
WI_IsAnyFlagSet(source, SuggestionsSource::CommandHistory | SuggestionsSource::QuickFixes | SuggestionsSource::Scrollback);
14661468
if (const auto& control{ _GetActiveControl() })
14671469
{
14681470
currentWorkingDirectory = control.CurrentWorkingDirectory();
@@ -1472,7 +1474,9 @@ namespace winrt::TerminalApp::implementation
14721474
context = control.CommandHistory();
14731475
if (context)
14741476
{
1475-
currentCommandline = context.CurrentCommandline();
1477+
winrt::hstring currentCommandline = context.CurrentCommandline();
1478+
winrt::hstring currentWordPrefix = context.CurrentWordPrefix();
1479+
filter = source == SuggestionsSource::Scrollback ? currentWordPrefix : currentCommandline;
14761480
}
14771481
}
14781482
}
@@ -1496,7 +1500,7 @@ namespace winrt::TerminalApp::implementation
14961500
// their settings file. Ask the ActionMap for those.
14971501
if (WI_IsFlagSet(source, SuggestionsSource::Tasks))
14981502
{
1499-
const auto tasks = co_await _settings.GlobalSettings().ActionMap().FilterToSnippets(currentCommandline, currentWorkingDirectory);
1503+
const auto tasks = co_await _settings.GlobalSettings().ActionMap().FilterToSnippets(filter, currentWorkingDirectory);
15001504
// ----- we may be on a background thread here -----
15011505
for (const auto& t : tasks)
15021506
{
@@ -1510,20 +1514,41 @@ namespace winrt::TerminalApp::implementation
15101514
if (WI_IsFlagSet(source, SuggestionsSource::CommandHistory) &&
15111515
context != nullptr)
15121516
{
1513-
const auto recentCommands = Command::HistoryToCommands(context.History(), currentCommandline, false, hstring{ L"\ue81c" });
1517+
const auto recentCommands = Command::HistoryToCommands(context.History(), filter, false, hstring{ L"\ue81c" });
15141518
for (const auto& t : recentCommands)
15151519
{
15161520
commandsCollection.push_back(t);
15171521
}
15181522
}
15191523

1524+
// Don't add this to All, or figure out a way to make it experimental
1525+
if (source == SuggestionsSource::Scrollback)
1526+
{
1527+
if (const auto termControl{_GetActiveControl()})
1528+
{
1529+
const auto scrollBackResults = termControl.SuggestionScrollBackSearch(realArgs.Regex());
1530+
1531+
std::unordered_set<winrt::hstring> seen;
1532+
seen.reserve(scrollBackResults.Size());
1533+
for (auto r : scrollBackResults)
1534+
{
1535+
if (seen.insert(r.Text).second)
1536+
{
1537+
auto c = Command::ScrollBackSuggestionToCommand(r.Text, filter, r.Row);
1538+
commandsCollection.push_back(c);
1539+
}
1540+
}
1541+
}
1542+
}
1543+
15201544
co_await wil::resume_foreground(Dispatcher());
15211545

15221546
// Open the palette with all these commands in it.
15231547
_OpenSuggestions(_GetActiveControl(),
15241548
winrt::single_threaded_vector<Command>(std::move(commandsCollection)),
15251549
SuggestionsMode::Palette,
1526-
currentCommandline);
1550+
filter,
1551+
sortResults);
15271552
}
15281553

15291554
void TerminalPage::_HandleColorSelection(const IInspectable& /*sender*/,

src/cascadia/TerminalApp/FilteredCommand.cpp

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,23 @@ using namespace winrt::Microsoft::Terminal::Settings::Model;
1919

2020
namespace winrt::TerminalApp::implementation
2121
{
22+
int32_t FilteredCommand::Ordinal()
23+
{
24+
return _ordinal;
25+
}
26+
27+
FilteredCommand::FilteredCommand(const winrt::TerminalApp::PaletteItem& item) :
28+
FilteredCommand(item, 0)
29+
{
30+
}
31+
2232
// This class is a wrapper of PaletteItem, that is used as an item of a filterable list in CommandPalette.
2333
// It manages a highlighted text that is computed by matching search filter characters to item name
24-
FilteredCommand::FilteredCommand(const winrt::TerminalApp::PaletteItem& item)
34+
FilteredCommand::FilteredCommand(const winrt::TerminalApp::PaletteItem& item, int32_t ordinal)
2535
{
2636
// Actually implement the ctor in _constructFilteredCommand
2737
_constructFilteredCommand(item);
38+
_ordinal = ordinal;
2839
}
2940

3041
// We need to actually implement the ctor in a separate helper. This is
@@ -111,6 +122,10 @@ namespace winrt::TerminalApp::implementation
111122

112123
if (firstWeight == secondWeight)
113124
{
125+
if (first.Ordinal() != second.Ordinal())
126+
{
127+
return first.Ordinal() < second.Ordinal();
128+
}
114129
const auto firstName = first.Item().Name();
115130
const auto secondName = second.Item().Name();
116131
return til::compare_linguistic_insensitive(firstName, secondName) < 0;

src/cascadia/TerminalApp/FilteredCommand.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ namespace winrt::TerminalApp::implementation
1919
{
2020
FilteredCommand() = default;
2121
FilteredCommand(const winrt::TerminalApp::PaletteItem& item);
22+
FilteredCommand(const winrt::TerminalApp::PaletteItem& item, int32_t ordinal);
2223

2324
virtual void UpdateFilter(std::shared_ptr<fzf::matcher::Pattern> pattern);
2425

@@ -29,13 +30,17 @@ namespace winrt::TerminalApp::implementation
2930
WINRT_OBSERVABLE_PROPERTY(winrt::Windows::Foundation::Collections::IVector<winrt::TerminalApp::HighlightedRun>, NameHighlights, PropertyChanged.raise);
3031
WINRT_OBSERVABLE_PROPERTY(int, Weight, PropertyChanged.raise);
3132

33+
public:
34+
int32_t Ordinal();
35+
3236
protected:
3337
void _constructFilteredCommand(const winrt::TerminalApp::PaletteItem& item);
3438

3539
private:
3640
std::shared_ptr<fzf::matcher::Pattern> _pattern;
3741
void _update();
3842
Windows::UI::Xaml::Data::INotifyPropertyChanged::PropertyChanged_revoker _itemChangedRevoker;
43+
int32_t _ordinal;
3944

4045
friend class TerminalAppLocalTests::FilteredCommandTests;
4146
};

src/cascadia/TerminalApp/FilteredCommand.idl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,11 @@ namespace TerminalApp
1010
{
1111
FilteredCommand();
1212
FilteredCommand(PaletteItem item);
13+
FilteredCommand(PaletteItem item, Int32 ordinal);
1314

1415
PaletteItem Item { get; };
1516
IVector<HighlightedRun> NameHighlights { get; };
1617
Int32 Weight;
18+
Int32 Ordinal { get; };
1719
}
1820
}

0 commit comments

Comments
 (0)