Skip to content
Merged
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 @@ -12,6 +12,7 @@

namespace Microsoft.PowerShell.EditorServices.Services.PowerShellContext
{
using System.Collections.Generic;
using System.Management.Automation;
using Microsoft.Extensions.Logging;

Expand Down Expand Up @@ -44,6 +45,15 @@ internal class PSReadLinePromptContext : IPromptContext
return [Microsoft.PowerShell.PSConsoleReadLine, Microsoft.PowerShell.PSReadLine2, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null]
}";

private static ExecutionOptions s_psrlExecutionOptions = new ExecutionOptions
{
WriteErrorsToHost = false,
WriteOutputToHost = false,
InterruptCommandPrompt = false,
AddToHistory = false,
IsReadLine = true,
};

private readonly PowerShellContextService _powerShellContext;

private readonly PromptNest _promptNest;
Expand All @@ -68,7 +78,6 @@ internal PSReadLinePromptContext(
_consoleReadLine = new ConsoleReadLine(powerShellContext);
_readLineProxy = readLineProxy;


_readLineProxy.OverrideReadKey(
intercept => ConsoleProxy.SafeReadKey(
intercept,
Expand All @@ -81,6 +90,7 @@ internal static bool TryGetPSReadLineProxy(
out PSReadLineProxy readLineProxy)
{
readLineProxy = null;
logger.LogTrace("Attempting to load PSReadLine");
using (var pwsh = PowerShell.Create())
{
pwsh.Runspace = runspace;
Expand All @@ -91,18 +101,20 @@ internal static bool TryGetPSReadLineProxy(

if (psReadLineType == null)
{
logger.LogWarning("PSReadLine unable to be loaded: {Reason}", pwsh.HadErrors ? pwsh.Streams.Error[0].ToString() : "<Unknown reason>");
return false;
}

try
{
readLineProxy = new PSReadLineProxy(psReadLineType, logger);
}
catch (InvalidOperationException)
catch (InvalidOperationException e)
{
// The Type we got back from PowerShell doesn't have the members we expected.
// Could be an older version, a custom build, or something a newer version with
// breaking changes.
logger.LogWarning("PSReadLine unable to be loaded: {Reason}", e);
return false;
}
}
Expand All @@ -119,38 +131,27 @@ public async Task<string> InvokeReadLineAsync(bool isCommandLine, CancellationTo
throw new TaskCanceledException();
}

try
if (!isCommandLine)
{
if (!isCommandLine)
{
return await _consoleReadLine.InvokeLegacyReadLineAsync(
isCommandLine: false,
_readLineCancellationSource.Token).ConfigureAwait(false);
}
return await _consoleReadLine.InvokeLegacyReadLineAsync(
isCommandLine: false,
_readLineCancellationSource.Token).ConfigureAwait(false);
}

var result = (await _powerShellContext.ExecuteCommandAsync<string>(
new PSCommand()
.AddScript(ReadLineScript)
.AddArgument(_readLineCancellationSource.Token),
errorMessages: null,
new ExecutionOptions()
{
WriteErrorsToHost = false,
WriteOutputToHost = false,
InterruptCommandPrompt = false,
AddToHistory = false,
IsReadLine = isCommandLine
}).ConfigureAwait(false))
.FirstOrDefault();
var readLineCommand = new PSCommand()
.AddScript(ReadLineScript)
.AddArgument(_readLineCancellationSource.Token);

return cancellationToken.IsCancellationRequested
? string.Empty
: result;
}
finally
{
_readLineCancellationSource = null;
}
IEnumerable<string> readLineResults = await _powerShellContext.ExecuteCommandAsync<string>(
readLineCommand,
errorMessages: null,
s_psrlExecutionOptions).ConfigureAwait(false);

string line = readLineResults.FirstOrDefault();

return cancellationToken.IsCancellationRequested
? string.Empty
: line;
}

public void AbortReadLine()
Expand Down