Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions MockPSConsole/MockPSConsole.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<OutputType>Exe</OutputType>
<RootNamespace>MockPSConsole</RootNamespace>
<AssemblyName>MockPSConsole</AssemblyName>
<TargetFrameworks>net461;net5.0</TargetFrameworks>
<TargetFrameworks>net461;net6.0</TargetFrameworks>
<FileAlignment>512</FileAlignment>
<ApplicationManifest>Program.manifest</ApplicationManifest>
<SuppressNETCoreSdkPreviewMessage>true</SuppressNETCoreSdkPreviewMessage>
Expand All @@ -17,8 +17,8 @@
<PackageReference Include="PowerShellStandard.Library" version="5.1.0" />
</ItemGroup>

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

<ItemGroup>
Expand Down
14 changes: 7 additions & 7 deletions PSReadLine.build.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ param(
[ValidateSet("Debug", "Release")]
[string]$Configuration = (property Configuration Release),

[ValidateSet("net461", "net5.0")]
[ValidateSet("net461", "net6.0")]
[string]$Framework,

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

if (-not $Framework)
{
$Framework = if ($PSVersionTable.PSEdition -eq "Core") { "net5.0" } else { "net461" }
$Framework = if ($PSVersionTable.PSEdition -eq "Core") { "net6.0" } else { "net461" }
}

Write-Verbose "Building for '$Framework'" -Verbose
Expand Down Expand Up @@ -65,9 +65,9 @@ $mockPSConsoleParams = @{
Synopsis: Build the Polyfiller assembly
#>
task BuildPolyfiller @polyFillerParams -If ($Framework -eq "net461") {
## Build both "net461" and "net5.0"
## Build both "net461" and "net6.0"
exec { dotnet publish -f "net461" -c $Configuration Polyfill }
exec { dotnet publish -f "net5.0" -c $Configuration Polyfill }
exec { dotnet publish -f "net6.0" -c $Configuration Polyfill }
}

<#
Expand Down Expand Up @@ -132,12 +132,12 @@ task LayoutModule BuildPolyfiller, BuildMainModule, {
if (-not (Test-Path "$targetDir/net461")) {
New-Item "$targetDir/net461" -ItemType Directory -Force > $null
}
if (-not (Test-Path "$targetDir/net5.0")) {
New-Item "$targetDir/net5.0" -ItemType Directory -Force > $null
if (-not (Test-Path "$targetDir/net6plus")) {
New-Item "$targetDir/net6plus" -ItemType Directory -Force > $null
}

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

$binPath = "PSReadLine/bin/$Configuration/$Framework/publish"
Expand Down
2 changes: 1 addition & 1 deletion PSReadLine/OnImportAndRemove.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ private static Assembly ResolveAssembly(object sender, ResolveEventArgs args)
}

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

return Assembly.LoadFrom(path);
Expand Down
2 changes: 1 addition & 1 deletion PSReadLine/Options.cs
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ private void SetOptionsInternal(SetPSReadLineOption options)
}

bool notTest = ReferenceEquals(_mockableMethods, this);
if ((options.PredictionSource & PredictionSource.Plugin) != 0 && Environment.Version.Major < 5 && notTest)
if ((options.PredictionSource & PredictionSource.Plugin) != 0 && Environment.Version.Major < 6 && notTest)
{
throw new ArgumentException(PSReadLineResources.PredictionPluginNotSupported);
}
Expand Down
6 changes: 3 additions & 3 deletions PSReadLine/PSReadLine.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<FileVersion>2.2.0</FileVersion>
<InformationalVersion>2.2.0-beta1</InformationalVersion>
<CheckForOverflowUnderflow>true</CheckForOverflowUnderflow>
<TargetFrameworks>net461;net5.0</TargetFrameworks>
<TargetFrameworks>net461;net6.0</TargetFrameworks>
<SuppressNETCoreSdkPreviewMessage>true</SuppressNETCoreSdkPreviewMessage>
<LangVersion>9.0</LangVersion>
</PropertyGroup>
Expand All @@ -20,8 +20,8 @@
<ProjectReference Include="..\Polyfill\Polyfill.csproj" />
</ItemGroup>

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

<ItemGroup>
Expand Down
2 changes: 2 additions & 0 deletions PSReadLine/PSReadLine.sln
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ VisualStudioVersion = 15.0.26730.12
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PSReadLine", "PSReadLine.csproj", "{615788CB-1B9A-4B34-97B3-4608686E59CA}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Polyfill", "..\Polyfill\Polyfill.csproj", "{DE521A7D-A3BE-4A07-BE75-5AB7D87E799D}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MockPSConsole", "..\MockPSConsole\MockPSConsole.csproj", "{08218B1A-8B85-4722-9E3F-4D6C0BF58AD8}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PSReadLine.Tests", "..\test\PSReadLine.Tests.csproj", "{8ED51D01-158C-4B29-824A-35B9B861E45A}"
Expand Down
2 changes: 1 addition & 1 deletion PSReadLine/PSReadLineResources.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion PSReadLine/PSReadLineResources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -815,7 +815,7 @@ Or not saving history with:
<value>The predictive suggestion feature cannot be enabled because the console output doesn't support virtual terminal processing or it's redirected.</value>
</data>
<data name="PredictionPluginNotSupported" xml:space="preserve">
<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>
<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>
</data>
<data name="DeleteEndOfBufferDescription" xml:space="preserve">
<value>Delete the current logical line and up to the end of the multiline buffer</value>
Expand Down
13 changes: 10 additions & 3 deletions PSReadLine/Prediction.Entry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,21 @@ public partial class PSConsoleReadLine
private struct SuggestionEntry
{
internal readonly Guid PredictorId;
internal readonly uint? PredictorSession;
internal readonly string Source;
internal readonly string SuggestionText;
internal readonly int InputMatchIndex;

internal SuggestionEntry(string soruce, Guid id, string suggestion, int matchIndex)
internal SuggestionEntry(string suggestion, int matchIndex)
: this(source: "History", predictorId: Guid.Empty, predictorSession: null, suggestion, matchIndex)
{
Source = soruce;
PredictorId = id;
}

internal SuggestionEntry(string source, Guid predictorId, uint? predictorSession, string suggestion, int matchIndex)
{
Source = source;
PredictorId = predictorId;
PredictorSession = predictorSession;
SuggestionText = suggestion;
InputMatchIndex = matchIndex;
}
Expand Down
45 changes: 36 additions & 9 deletions PSReadLine/Prediction.Views.cs
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,6 @@ protected string GetOneHistorySuggestion(string text)
/// <param name="count">Maximum number of results to return.</param>
protected List<SuggestionEntry> GetHistorySuggestions(string input, int count)
{
const string source = "History";
List<SuggestionEntry> results = null;
int remainingCount = count;

Expand Down Expand Up @@ -164,15 +163,15 @@ protected List<SuggestionEntry> GetHistorySuggestions(string input, int count)
_cacheHistorySet.Add(line);
if (matchIndex == 0)
{
results.Add(new SuggestionEntry(source, Guid.Empty, line, matchIndex));
results.Add(new SuggestionEntry(line, matchIndex));
if (--remainingCount == 0)
{
break;
}
}
else if (_cacheHistoryList.Count < remainingCount)
{
_cacheHistoryList.Add(new SuggestionEntry(source, Guid.Empty, line, matchIndex));
_cacheHistoryList.Add(new SuggestionEntry(line, matchIndex));
}
}

Expand Down Expand Up @@ -440,11 +439,19 @@ private void AggregateSuggestions()
break;
}

for (int i = 0; i < _cacheList2[index]; i++)
int num = _cacheList2[index];
for (int i = 0; i < num; i++)
{
string sugText = item.Suggestions[i].SuggestionText ?? string.Empty;
int matchIndex = sugText.IndexOf(_inputText, comparison);
_listItems.Add(new SuggestionEntry(item.Name, item.Id, sugText, matchIndex));
_listItems.Add(new SuggestionEntry(item.Name, item.Id, item.Session, sugText, matchIndex));
}

if (item.Session.HasValue)
{
// Send feedback only if the mini-session id is specified.
// When it's not specified, we consider the predictor doesn't accept feedback.
_singleton._mockableMethods.OnSuggestionDisplayed(item.Id, item.Session.Value, num);
}
}
}
Expand Down Expand Up @@ -517,9 +524,11 @@ internal override void OnSuggestionAccepted()
if (_listItems != null && _selectedIndex != -1)
{
var item = _listItems[_selectedIndex];
if (item.PredictorId != Guid.Empty)
if (item.PredictorSession.HasValue)
{
_singleton._mockableMethods.OnSuggestionAccepted(item.PredictorId, item.SuggestionText);
// Send feedback only if the mini-session id is specified.
// When it's not specified, we consider the predictor doesn't accept feedback.
_singleton._mockableMethods.OnSuggestionAccepted(item.PredictorId, item.PredictorSession.Value, item.SuggestionText);
}
}
}
Expand Down Expand Up @@ -577,6 +586,7 @@ internal void UpdateListSelection(int move)
private class PredictionInlineView : PredictionViewBase
{
private Guid _predictorId;
private uint? _predictorSession;
private string _suggestionText;
private string _lastInputText;
private bool _alreadyAccepted;
Expand Down Expand Up @@ -613,6 +623,7 @@ internal override void GetSuggestion(string userInput)
{
_suggestionText = GetOneHistorySuggestion(userInput);
_predictorId = Guid.Empty;
_predictorSession = null;
}
}
}
Expand All @@ -634,15 +645,27 @@ private void AggregateSuggestions()
continue;
}

int index = 0;
foreach (var sug in item.Suggestions)
{
if (sug.SuggestionText != null &&
sug.SuggestionText.StartsWith(_inputText, _singleton._options.HistoryStringComparison))
{
_predictorId = item.Id;
_predictorSession = item.Session;
_suggestionText = sug.SuggestionText;

if (_predictorSession.HasValue)
{
// Send feedback only if the mini-session id is specified.
// When it's not specified, we consider the predictor doesn't accept feedback.
_singleton._mockableMethods.OnSuggestionDisplayed(_predictorId, _predictorSession.Value, -index);
}

return;
}

index++;
}
}
}
Expand Down Expand Up @@ -673,10 +696,13 @@ internal override void OnSuggestionAccepted()
return;
}

if (!_alreadyAccepted && _suggestionText != null && _predictorId != Guid.Empty)
if (!_alreadyAccepted && _suggestionText != null && _predictorSession.HasValue)
{
_alreadyAccepted = true;
_singleton._mockableMethods.OnSuggestionAccepted(_predictorId, _suggestionText);

// Send feedback only if the mini-session id is specified.
// When it's not specified, we consider the predictor doesn't accept feedback.
_singleton._mockableMethods.OnSuggestionAccepted(_predictorId, _predictorSession.Value, _suggestionText);
}
}

Expand Down Expand Up @@ -725,6 +751,7 @@ internal override void Reset()
base.Reset();
_suggestionText = _lastInputText = null;
_predictorId = Guid.Empty;
_predictorSession = null;
_alreadyAccepted = false;
}

Expand Down
18 changes: 13 additions & 5 deletions PSReadLine/Prediction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,31 @@ namespace Microsoft.PowerShell
{
public partial class PSConsoleReadLine
{
private const string PSReadLine = "PSReadLine";

// Stub helper methods so prediction can be mocked
[ExcludeFromCodeCoverage]
Task<List<PredictionResult>> IPSConsoleReadLineMockableMethods.PredictInput(Ast ast, Token[] tokens)
{
return CommandPrediction.PredictInput(ast, tokens);
return CommandPrediction.PredictInput(PSReadLine, ast, tokens);
}

[ExcludeFromCodeCoverage]
void IPSConsoleReadLineMockableMethods.OnCommandLineAccepted(IReadOnlyList<string> history)
void IPSConsoleReadLineMockableMethods.OnSuggestionDisplayed(Guid predictorId, uint session, int countOrIndex)
{
CommandPrediction.OnCommandLineAccepted(history);
CommandPrediction.OnSuggestionDisplayed(PSReadLine, predictorId, session, countOrIndex);
}

[ExcludeFromCodeCoverage]
void IPSConsoleReadLineMockableMethods.OnSuggestionAccepted(Guid predictorId, string suggestionText)
void IPSConsoleReadLineMockableMethods.OnSuggestionAccepted(Guid predictorId, uint session, string suggestionText)
{
CommandPrediction.OnSuggestionAccepted(PSReadLine, predictorId, session, suggestionText);
}

[ExcludeFromCodeCoverage]
void IPSConsoleReadLineMockableMethods.OnCommandLineAccepted(IReadOnlyList<string> history)
{
CommandPrediction.OnSuggestionAccepted(predictorId, suggestionText);
CommandPrediction.OnCommandLineAccepted(PSReadLine, history);
}

private readonly Prediction _prediction;
Expand Down
3 changes: 2 additions & 1 deletion PSReadLine/PublicAPI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ public interface IPSConsoleReadLineMockableMethods
bool RunspaceIsRemote(Runspace runspace);
Task<List<PredictionResult>> PredictInput(Ast ast, Token[] tokens);
void OnCommandLineAccepted(IReadOnlyList<string> history);
void OnSuggestionAccepted(Guid predictorId, string suggestionText);
void OnSuggestionDisplayed(Guid predictorId, uint session, int countOrIndex);
void OnSuggestionAccepted(Guid predictorId, uint session, string suggestionText);
void RenderFullHelp(string content, string regexPatternToScrollTo);
object GetDynamicHelpContent(string commandName, string parameterName, bool isFullHelp);
}
Expand Down
2 changes: 1 addition & 1 deletion PSReadLine/ReadLine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -668,7 +668,7 @@ private PSConsoleReadLine()
}
if (hostName == null)
{
hostName = "PSReadLine";
hostName = PSReadLine;
}
_options = new PSConsoleReadLineOptions(hostName);
_prediction = new Prediction(this);
Expand Down
Loading