Skip to content

Utilize the AddToHistory delegate from PSRL proxy #1823

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 10, 2022
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
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,7 @@ namespace Microsoft.PowerShell.EditorServices.Services.PowerShell.Console
internal interface IReadLine
{
string ReadLine(CancellationToken cancellationToken);

void AddToHistory(string historyEntry);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ public override string ReadLine(CancellationToken cancellationToken) => _psesHos
InvokePSReadLine,
cancellationToken);

public override void AddToHistory(string historyEntry) => _psrlProxy.AddToHistory(historyEntry);

protected override ConsoleKeyInfo ReadKey(CancellationToken cancellationToken) => _psesHost.ReadKey(intercept: true, cancellationToken);

private string InvokePSReadLine(CancellationToken cancellationToken)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ namespace Microsoft.PowerShell.EditorServices.Services.PowerShell.Console

internal abstract class TerminalReadLine : IReadLine
{
public virtual void AddToHistory(string historyEntry)
{
// No-op by default. If the ReadLine provider is not PSRL then history is automatically
// added as part of the invocation process.
}

public abstract string ReadLine(CancellationToken cancellationToken);

protected abstract ConsoleKeyInfo ReadKey(CancellationToken cancellationToken);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,6 @@ public record PowerShellExecutionOptions : ExecutionOptions
public bool WriteInputToHost { get; init; }
public bool ThrowOnError { get; init; } = true;
public bool AddToHistory { get; init; }
internal bool FromRepl { get; init; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ private static bool IsPromptCommand(PSCommand command)
private IReadOnlyList<TResult> ExecuteNormally(CancellationToken cancellationToken)
{
_frame = _psesHost.CurrentFrame;
MaybeAddToHistory(_psCommand);
if (PowerShellExecutionOptions.WriteOutputToHost)
{
_psCommand.AddOutputCommand();
Expand Down Expand Up @@ -187,6 +188,7 @@ private IReadOnlyList<TResult> ExecuteInDebugger(CancellationToken cancellationT
cancellationToken.Register(CancelDebugExecution);

PSDataCollection<PSObject> outputCollection = new();
MaybeAddToHistory(_psCommand);

// Out-Default doesn't work as needed in the debugger
// Instead we add Out-String to the command and collect results in a PSDataCollection
Expand Down Expand Up @@ -353,6 +355,33 @@ private void CancelNormalExecution()
}
}

private void MaybeAddToHistory(PSCommand command)
{
// Do not add PSES internal commands to history. Also exclude input that came from the
// REPL (e.g. PSReadLine) as it handles history itself in that scenario.
if (PowerShellExecutionOptions is { AddToHistory: false } or { FromRepl: true })
{
return;
}

// Only add pure script commands with no arguments to interactive history.
if (command.Commands is { Count: not 1 }
|| command.Commands[0] is { Parameters.Count: not 0 } or { IsScript: false })
{
return;
}

try
{
_psesHost.AddToHistory(command.Commands[0].CommandText);
}
catch
{
// Ignore exceptions as the user can register a scriptblock predicate that
// determines if the command should be added to history.
}
}

private void CancelDebugExecution()
{
if (_pwsh.Runspace.RunspaceStateInfo.IsUsable())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,8 @@ public void InvokePSDelegate(string representation, ExecutionOptions executionOp
task.ExecuteAndGetResult(cancellationToken);
}

internal void AddToHistory(string historyEntry) => _readLineProvider.ReadLine.AddToHistory(historyEntry);

internal Task LoadHostProfilesAsync(CancellationToken cancellationToken)
{
// NOTE: This is a special task run on startup!
Expand Down Expand Up @@ -918,7 +920,8 @@ private void InvokeInput(string input, CancellationToken cancellationToken)
{
AddToHistory = true,
ThrowOnError = false,
WriteOutputToHost = true
WriteOutputToHost = true,
FromRepl = true,
},
cancellationToken);
}
Expand Down