Skip to content

Commit 8df5b6b

Browse files
authored
Update PSReadLine corresponding to the prediction interface update (#2225)
1 parent 4b239f2 commit 8df5b6b

21 files changed

+202
-53
lines changed

MockPSConsole/MockPSConsole.csproj

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<OutputType>Exe</OutputType>
55
<RootNamespace>MockPSConsole</RootNamespace>
66
<AssemblyName>MockPSConsole</AssemblyName>
7-
<TargetFrameworks>net461;net5.0</TargetFrameworks>
7+
<TargetFrameworks>net461;net6.0</TargetFrameworks>
88
<FileAlignment>512</FileAlignment>
99
<ApplicationManifest>Program.manifest</ApplicationManifest>
1010
<SuppressNETCoreSdkPreviewMessage>true</SuppressNETCoreSdkPreviewMessage>
@@ -17,8 +17,8 @@
1717
<PackageReference Include="PowerShellStandard.Library" version="5.1.0" />
1818
</ItemGroup>
1919

20-
<ItemGroup Condition="'$(TargetFramework)' == 'net5.0'">
21-
<PackageReference Include="Microsoft.PowerShell.SDK" version="7.1.0" />
20+
<ItemGroup Condition="'$(TargetFramework)' == 'net6.0'">
21+
<PackageReference Include="Microsoft.PowerShell.SDK" version="7.2.0-preview.3" />
2222
</ItemGroup>
2323

2424
<ItemGroup>

PSReadLine.build.ps1

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ param(
1919
[ValidateSet("Debug", "Release")]
2020
[string]$Configuration = (property Configuration Release),
2121

22-
[ValidateSet("net461", "net5.0")]
22+
[ValidateSet("net461", "net6.0")]
2323
[string]$Framework,
2424

2525
[switch]$CheckHelpContent
@@ -32,7 +32,7 @@ $targetDir = "bin/$Configuration/PSReadLine"
3232

3333
if (-not $Framework)
3434
{
35-
$Framework = if ($PSVersionTable.PSEdition -eq "Core") { "net5.0" } else { "net461" }
35+
$Framework = if ($PSVersionTable.PSEdition -eq "Core") { "net6.0" } else { "net461" }
3636
}
3737

3838
Write-Verbose "Building for '$Framework'" -Verbose
@@ -65,9 +65,9 @@ $mockPSConsoleParams = @{
6565
Synopsis: Build the Polyfiller assembly
6666
#>
6767
task BuildPolyfiller @polyFillerParams -If ($Framework -eq "net461") {
68-
## Build both "net461" and "net5.0"
68+
## Build both "net461" and "net6.0"
6969
exec { dotnet publish -f "net461" -c $Configuration Polyfill }
70-
exec { dotnet publish -f "net5.0" -c $Configuration Polyfill }
70+
exec { dotnet publish -f "net6.0" -c $Configuration Polyfill }
7171
}
7272

7373
<#
@@ -132,12 +132,12 @@ task LayoutModule BuildPolyfiller, BuildMainModule, {
132132
if (-not (Test-Path "$targetDir/net461")) {
133133
New-Item "$targetDir/net461" -ItemType Directory -Force > $null
134134
}
135-
if (-not (Test-Path "$targetDir/net5.0")) {
136-
New-Item "$targetDir/net5.0" -ItemType Directory -Force > $null
135+
if (-not (Test-Path "$targetDir/net6plus")) {
136+
New-Item "$targetDir/net6plus" -ItemType Directory -Force > $null
137137
}
138138

139139
Copy-Item "Polyfill/bin/$Configuration/net461/Microsoft.PowerShell.PSReadLine.Polyfiller.dll" "$targetDir/net461" -Force
140-
Copy-Item "Polyfill/bin/$Configuration/net5.0/Microsoft.PowerShell.PSReadLine.Polyfiller.dll" "$targetDir/net5.0" -Force
140+
Copy-Item "Polyfill/bin/$Configuration/net6.0/Microsoft.PowerShell.PSReadLine.Polyfiller.dll" "$targetDir/net6plus" -Force
141141
}
142142

143143
$binPath = "PSReadLine/bin/$Configuration/$Framework/publish"

PSReadLine/OnImportAndRemove.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ private static Assembly ResolveAssembly(object sender, ResolveEventArgs args)
2828
}
2929

3030
string root = Path.GetDirectoryName(typeof(OnModuleImportAndRemove).Assembly.Location);
31-
string subd = (Environment.Version.Major >= 5) ? "net5.0" : "net461";
31+
string subd = (Environment.Version.Major >= 6) ? "net6plus" : "net461";
3232
string path = Path.Combine(root, subd, "Microsoft.PowerShell.PSReadLine.Polyfiller.dll");
3333

3434
return Assembly.LoadFrom(path);

PSReadLine/Options.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ private void SetOptionsInternal(SetPSReadLineOption options)
143143
}
144144

145145
bool notTest = ReferenceEquals(_mockableMethods, this);
146-
if ((options.PredictionSource & PredictionSource.Plugin) != 0 && Environment.Version.Major < 5 && notTest)
146+
if ((options.PredictionSource & PredictionSource.Plugin) != 0 && Environment.Version.Major < 6 && notTest)
147147
{
148148
throw new ArgumentException(PSReadLineResources.PredictionPluginNotSupported);
149149
}

PSReadLine/PSReadLine.csproj

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
<FileVersion>2.2.0</FileVersion>
1010
<InformationalVersion>2.2.0-beta1</InformationalVersion>
1111
<CheckForOverflowUnderflow>true</CheckForOverflowUnderflow>
12-
<TargetFrameworks>net461;net5.0</TargetFrameworks>
12+
<TargetFrameworks>net461;net6.0</TargetFrameworks>
1313
<SuppressNETCoreSdkPreviewMessage>true</SuppressNETCoreSdkPreviewMessage>
1414
<LangVersion>9.0</LangVersion>
1515
</PropertyGroup>
@@ -20,8 +20,8 @@
2020
<ProjectReference Include="..\Polyfill\Polyfill.csproj" />
2121
</ItemGroup>
2222

23-
<ItemGroup Condition="'$(TargetFramework)' == 'net5.0'">
24-
<PackageReference Include="System.Management.Automation" Version="7.1.0" />
23+
<ItemGroup Condition="'$(TargetFramework)' == 'net6.0'">
24+
<PackageReference Include="System.Management.Automation" Version="7.2.0-preview.3" />
2525
</ItemGroup>
2626

2727
<ItemGroup>

PSReadLine/PSReadLine.sln

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ VisualStudioVersion = 15.0.26730.12
55
MinimumVisualStudioVersion = 10.0.40219.1
66
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PSReadLine", "PSReadLine.csproj", "{615788CB-1B9A-4B34-97B3-4608686E59CA}"
77
EndProject
8+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Polyfill", "..\Polyfill\Polyfill.csproj", "{DE521A7D-A3BE-4A07-BE75-5AB7D87E799D}"
9+
EndProject
810
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MockPSConsole", "..\MockPSConsole\MockPSConsole.csproj", "{08218B1A-8B85-4722-9E3F-4D6C0BF58AD8}"
911
EndProject
1012
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PSReadLine.Tests", "..\test\PSReadLine.Tests.csproj", "{8ED51D01-158C-4B29-824A-35B9B861E45A}"

PSReadLine/PSReadLineResources.Designer.cs

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

PSReadLine/PSReadLineResources.resx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -815,7 +815,7 @@ Or not saving history with:
815815
<value>The predictive suggestion feature cannot be enabled because the console output doesn't support virtual terminal processing or it's redirected.</value>
816816
</data>
817817
<data name="PredictionPluginNotSupported" xml:space="preserve">
818-
<value>The prediction plugin source is not supported in this version of PowerShell. The 7.1 or a higher version of PowerShell is required to use this source.</value>
818+
<value>The prediction plugin source is not supported in this version of PowerShell. The 7.2 or a higher version of PowerShell is required to use this source.</value>
819819
</data>
820820
<data name="DeleteEndOfBufferDescription" xml:space="preserve">
821821
<value>Delete the current logical line and up to the end of the multiline buffer</value>

PSReadLine/Prediction.Entry.cs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,21 @@ public partial class PSConsoleReadLine
1515
private struct SuggestionEntry
1616
{
1717
internal readonly Guid PredictorId;
18+
internal readonly uint? PredictorSession;
1819
internal readonly string Source;
1920
internal readonly string SuggestionText;
2021
internal readonly int InputMatchIndex;
2122

22-
internal SuggestionEntry(string soruce, Guid id, string suggestion, int matchIndex)
23+
internal SuggestionEntry(string suggestion, int matchIndex)
24+
: this(source: "History", predictorId: Guid.Empty, predictorSession: null, suggestion, matchIndex)
2325
{
24-
Source = soruce;
25-
PredictorId = id;
26+
}
27+
28+
internal SuggestionEntry(string source, Guid predictorId, uint? predictorSession, string suggestion, int matchIndex)
29+
{
30+
Source = source;
31+
PredictorId = predictorId;
32+
PredictorSession = predictorSession;
2633
SuggestionText = suggestion;
2734
InputMatchIndex = matchIndex;
2835
}

PSReadLine/Prediction.Views.cs

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,6 @@ protected string GetOneHistorySuggestion(string text)
128128
/// <param name="count">Maximum number of results to return.</param>
129129
protected List<SuggestionEntry> GetHistorySuggestions(string input, int count)
130130
{
131-
const string source = "History";
132131
List<SuggestionEntry> results = null;
133132
int remainingCount = count;
134133

@@ -164,15 +163,15 @@ protected List<SuggestionEntry> GetHistorySuggestions(string input, int count)
164163
_cacheHistorySet.Add(line);
165164
if (matchIndex == 0)
166165
{
167-
results.Add(new SuggestionEntry(source, Guid.Empty, line, matchIndex));
166+
results.Add(new SuggestionEntry(line, matchIndex));
168167
if (--remainingCount == 0)
169168
{
170169
break;
171170
}
172171
}
173172
else if (_cacheHistoryList.Count < remainingCount)
174173
{
175-
_cacheHistoryList.Add(new SuggestionEntry(source, Guid.Empty, line, matchIndex));
174+
_cacheHistoryList.Add(new SuggestionEntry(line, matchIndex));
176175
}
177176
}
178177

@@ -440,11 +439,19 @@ private void AggregateSuggestions()
440439
break;
441440
}
442441

443-
for (int i = 0; i < _cacheList2[index]; i++)
442+
int num = _cacheList2[index];
443+
for (int i = 0; i < num; i++)
444444
{
445445
string sugText = item.Suggestions[i].SuggestionText ?? string.Empty;
446446
int matchIndex = sugText.IndexOf(_inputText, comparison);
447-
_listItems.Add(new SuggestionEntry(item.Name, item.Id, sugText, matchIndex));
447+
_listItems.Add(new SuggestionEntry(item.Name, item.Id, item.Session, sugText, matchIndex));
448+
}
449+
450+
if (item.Session.HasValue)
451+
{
452+
// Send feedback only if the mini-session id is specified.
453+
// When it's not specified, we consider the predictor doesn't accept feedback.
454+
_singleton._mockableMethods.OnSuggestionDisplayed(item.Id, item.Session.Value, num);
448455
}
449456
}
450457
}
@@ -517,9 +524,11 @@ internal override void OnSuggestionAccepted()
517524
if (_listItems != null && _selectedIndex != -1)
518525
{
519526
var item = _listItems[_selectedIndex];
520-
if (item.PredictorId != Guid.Empty)
527+
if (item.PredictorSession.HasValue)
521528
{
522-
_singleton._mockableMethods.OnSuggestionAccepted(item.PredictorId, item.SuggestionText);
529+
// Send feedback only if the mini-session id is specified.
530+
// When it's not specified, we consider the predictor doesn't accept feedback.
531+
_singleton._mockableMethods.OnSuggestionAccepted(item.PredictorId, item.PredictorSession.Value, item.SuggestionText);
523532
}
524533
}
525534
}
@@ -577,6 +586,7 @@ internal void UpdateListSelection(int move)
577586
private class PredictionInlineView : PredictionViewBase
578587
{
579588
private Guid _predictorId;
589+
private uint? _predictorSession;
580590
private string _suggestionText;
581591
private string _lastInputText;
582592
private bool _alreadyAccepted;
@@ -613,6 +623,7 @@ internal override void GetSuggestion(string userInput)
613623
{
614624
_suggestionText = GetOneHistorySuggestion(userInput);
615625
_predictorId = Guid.Empty;
626+
_predictorSession = null;
616627
}
617628
}
618629
}
@@ -634,15 +645,27 @@ private void AggregateSuggestions()
634645
continue;
635646
}
636647

648+
int index = 0;
637649
foreach (var sug in item.Suggestions)
638650
{
639651
if (sug.SuggestionText != null &&
640652
sug.SuggestionText.StartsWith(_inputText, _singleton._options.HistoryStringComparison))
641653
{
642654
_predictorId = item.Id;
655+
_predictorSession = item.Session;
643656
_suggestionText = sug.SuggestionText;
657+
658+
if (_predictorSession.HasValue)
659+
{
660+
// Send feedback only if the mini-session id is specified.
661+
// When it's not specified, we consider the predictor doesn't accept feedback.
662+
_singleton._mockableMethods.OnSuggestionDisplayed(_predictorId, _predictorSession.Value, -index);
663+
}
664+
644665
return;
645666
}
667+
668+
index++;
646669
}
647670
}
648671
}
@@ -673,10 +696,13 @@ internal override void OnSuggestionAccepted()
673696
return;
674697
}
675698

676-
if (!_alreadyAccepted && _suggestionText != null && _predictorId != Guid.Empty)
699+
if (!_alreadyAccepted && _suggestionText != null && _predictorSession.HasValue)
677700
{
678701
_alreadyAccepted = true;
679-
_singleton._mockableMethods.OnSuggestionAccepted(_predictorId, _suggestionText);
702+
703+
// Send feedback only if the mini-session id is specified.
704+
// When it's not specified, we consider the predictor doesn't accept feedback.
705+
_singleton._mockableMethods.OnSuggestionAccepted(_predictorId, _predictorSession.Value, _suggestionText);
680706
}
681707
}
682708

@@ -725,6 +751,7 @@ internal override void Reset()
725751
base.Reset();
726752
_suggestionText = _lastInputText = null;
727753
_predictorId = Guid.Empty;
754+
_predictorSession = null;
728755
_alreadyAccepted = false;
729756
}
730757

0 commit comments

Comments
 (0)