Skip to content

Commit bf06ccc

Browse files
committed
Hook up legacy readline support in the host
1 parent 8a155d5 commit bf06ccc

File tree

2 files changed

+25
-9
lines changed

2 files changed

+25
-9
lines changed

src/PowerShellEditorServices/Services/PowerShell/Console/LegacyReadLine.cs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,20 +20,16 @@ internal class LegacyReadLine : TerminalReadLine
2020
{
2121
private readonly PsesInternalHost _psesHost;
2222

23-
private readonly IPowerShellDebugContext _debugContext;
24-
2523
private readonly Task[] _readKeyTasks;
2624

2725
private Func<bool, ConsoleKeyInfo> _readKeyFunc;
2826

2927
private Action _onIdleAction;
3028

3129
public LegacyReadLine(
32-
PsesInternalHost psesHost,
33-
IPowerShellDebugContext debugContext)
30+
PsesInternalHost psesHost)
3431
{
3532
_psesHost = psesHost;
36-
_debugContext = debugContext;
3733
_readKeyTasks = new Task[2];
3834
}
3935

@@ -86,7 +82,7 @@ public override string ReadLine(CancellationToken cancellationToken)
8682

8783
// TODO: This logic should be moved to AstOperations or similar!
8884

89-
if (_debugContext.IsStopped)
85+
if (_psesHost.DebugContext.IsStopped)
9086
{
9187
PSCommand command = new PSCommand()
9288
.AddCommand("TabExpansion2")

src/PowerShellEditorServices/Services/PowerShell/Host/PsesInternalHost.cs

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -625,10 +625,14 @@ private static PowerShell CreatePowerShellForRunspace(Runspace runspace)
625625

626626
var engineIntrinsics = (EngineIntrinsics)runspace.SessionStateProxy.GetVariable("ExecutionContext");
627627

628-
if (hostStartupInfo.ConsoleReplEnabled && !hostStartupInfo.UsesLegacyReadLine)
628+
if (hostStartupInfo.ConsoleReplEnabled)
629629
{
630-
var psrlProxy = PSReadLineProxy.LoadAndCreate(_loggerFactory, pwsh);
631-
var readLine = new PsrlReadLine(psrlProxy, this, engineIntrinsics);
630+
// If we've been configured to use it, or if we can't load PSReadLine, use the legacy readline
631+
if (hostStartupInfo.UsesLegacyReadLine || !TryLoadPSReadLine(pwsh, engineIntrinsics, out IReadLine readLine))
632+
{
633+
readLine = new LegacyReadLine(this);
634+
}
635+
632636
readLine.TryOverrideReadKey(ReadKey);
633637
readLine.TryOverrideIdleHandler(OnPowerShellIdle);
634638
readLineProvider.OverrideReadLine(readLine);
@@ -823,6 +827,22 @@ private Task PopOrReinitializeRunspaceAsync()
823827
CancellationToken.None);
824828
}
825829

830+
private bool TryLoadPSReadLine(PowerShell pwsh, EngineIntrinsics engineIntrinsics, out IReadLine psrlReadLine)
831+
{
832+
psrlReadLine = null;
833+
try
834+
{
835+
var psrlProxy = PSReadLineProxy.LoadAndCreate(_loggerFactory, pwsh);
836+
psrlReadLine = new PsrlReadLine(psrlProxy, this, engineIntrinsics);
837+
return true;
838+
}
839+
catch (Exception e)
840+
{
841+
_logger.LogError(e, "Unable to load PSReadLine. Will fall back to legacy readline implementation.");
842+
return false;
843+
}
844+
}
845+
826846
private record RunspaceFrame(
827847
Runspace Runspace,
828848
RunspaceInfo RunspaceInfo);

0 commit comments

Comments
 (0)