Skip to content

Commit 2eb92ae

Browse files
committed
Remove WriteWithPrompt workaround
1 parent 87b3c9c commit 2eb92ae

File tree

2 files changed

+17
-24
lines changed

2 files changed

+17
-24
lines changed

src/PowerShellEditorServices/Services/PowerShell/Execution/SynchronousPowerShellTask.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public override IReadOnlyList<TResult> Run(CancellationToken cancellationToken)
6262

6363
if (PowerShellExecutionOptions.WriteInputToHost)
6464
{
65-
_psesHost.WriteWithPrompt(_psCommand, cancellationToken);
65+
_psesHost.UI.WriteLine(_psCommand.GetInvocationText());
6666
}
6767

6868
return _pwsh.Runspace.Debugger.InBreakpoint

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

+16-23
Original file line numberDiff line numberDiff line change
@@ -757,18 +757,22 @@ private void DoOneRepl(CancellationToken cancellationToken)
757757

758758
// If the user input was empty it's because:
759759
// - the user provided no input
760-
// - the readline task was canceled
761-
// - CtrlC was sent to readline (which does not propagate a cancellation)
760+
// - the ReadLine task was canceled
761+
// - CtrlC was sent to ReadLine (which does not propagate a cancellation)
762762
//
763-
// In any event there's nothing to run in PowerShell, so we just loop back to the prompt again.
764-
// However, we must distinguish the last two scenarios, since PSRL will not print a new line in those cases.
763+
// In any event there's nothing to run in PowerShell, so we just loop back to the
764+
// prompt again. However, PSReadLine will not print a newline for CtrlC, so we print
765+
// one, but we do not want to print one if the ReadLine task was canceled.
765766
if (string.IsNullOrEmpty(userInput))
766767
{
767-
if (cancellationToken.IsCancellationRequested || LastKeyWasCtrlC())
768+
if (LastKeyWasCtrlC())
768769
{
769770
UI.WriteLine();
770771
}
771-
return;
772+
// Propogate cancellation if that's what happened, since ReadLine won't.
773+
// TODO: We may not need to do this at all.
774+
cancellationToken.ThrowIfCancellationRequested();
775+
return; // Task wasn't canceled but there was no input.
772776
}
773777

774778
InvokeInput(userInput, cancellationToken);
@@ -782,10 +786,8 @@ private void DoOneRepl(CancellationToken cancellationToken)
782786
{
783787
throw;
784788
}
785-
catch (FlowControlException)
786-
{
787-
// Do nothing, a break or continue statement was used outside of a loop.
788-
}
789+
// Do nothing, a break or continue statement was used outside of a loop.
790+
catch (FlowControlException) { }
789791
catch (Exception e)
790792
{
791793
UI.WriteErrorLine($"An error occurred while running the REPL loop:{Environment.NewLine}{e}");
@@ -829,25 +831,16 @@ private string GetPrompt(CancellationToken cancellationToken)
829831
return prompt;
830832
}
831833

832-
/// <summary>
833-
/// This is used to write the invocation text of a command with the user's prompt so that,
834-
/// for example, F8 (evaluate selection) appears as if the user typed it. Used when
835-
/// 'WriteInputToHost' is true.
836-
/// </summary>
837-
/// <param name="command">The PSCommand we'll print after the prompt.</param>
838-
/// <param name="cancellationToken"></param>
839-
public void WriteWithPrompt(PSCommand command, CancellationToken cancellationToken)
840-
{
841-
UI.Write(GetPrompt(cancellationToken));
842-
UI.WriteLine(command.GetInvocationText());
843-
}
844-
845834
private string InvokeReadLine(CancellationToken cancellationToken)
846835
{
847836
cancellationToken.ThrowIfCancellationRequested();
848837
try
849838
{
839+
// TODO: If we can pass the cancellation token to ReadKey directly in PSReadLine, we
840+
// can remove this logic.
841+
_logger.LogDebug("Saving ReadKey cancellation token and invoking ReadLine");
850842
_readKeyCancellationToken = cancellationToken;
843+
cancellationToken.ThrowIfCancellationRequested();
851844
return _readLineProvider.ReadLine.ReadLine(cancellationToken);
852845
}
853846
finally

0 commit comments

Comments
 (0)