diff --git a/src/PowerShellEditorServices/Services/PowerShellContext/PowerShellContextService.cs b/src/PowerShellEditorServices/Services/PowerShellContext/PowerShellContextService.cs index 30eff8760..6780b019a 100644 --- a/src/PowerShellEditorServices/Services/PowerShellContext/PowerShellContextService.cs +++ b/src/PowerShellEditorServices/Services/PowerShellContext/PowerShellContextService.cs @@ -199,7 +199,7 @@ public static PowerShellContextService Create( EditorServicesPSHostUserInterface hostUserInterface = hostStartupInfo.ConsoleReplEnabled - ? (EditorServicesPSHostUserInterface)new TerminalPSHostUserInterface(powerShellContext, logger, hostStartupInfo.PSHost) + ? (EditorServicesPSHostUserInterface) new TerminalPSHostUserInterface(powerShellContext, hostStartupInfo.PSHost, shouldUsePSReadLine, logger) : new ProtocolPSHostUserInterface(languageServer, powerShellContext, logger); EditorServicesPSHost psHost = @@ -680,7 +680,9 @@ public async Task> ExecuteCommandAsync( runspaceHandle = await this.GetRunspaceHandleAsync(executionOptions.IsReadLine).ConfigureAwait(false); if (executionOptions.WriteInputToHost) { - this.WriteOutput(psCommand.Commands[0].CommandText, true); + this.WriteOutput( + psCommand.Commands[0].CommandText, + includeNewLine: true); } if (executionTarget == ExecutionTarget.Debugger) @@ -1074,18 +1076,17 @@ public async Task ExecuteScriptWithArgsAsync(string script, string arguments = n command.AddCommand(script, false); } - if (writeInputToHost) - { - this.WriteOutput( - script + Environment.NewLine, - true); - } await this.ExecuteCommandAsync( - command, - errorMessages: null, - sendOutputToHost: true, - addToHistory: true).ConfigureAwait(false); + command, + errorMessages: null, + new ExecutionOptions + { + WriteInputToHost = true, + WriteOutputToHost = true, + WriteErrorsToHost = true, + AddToHistory = true, + }).ConfigureAwait(false); } /// diff --git a/src/PowerShellEditorServices/Services/PowerShellContext/Session/Host/EditorServicesPSHostUserInterface.cs b/src/PowerShellEditorServices/Services/PowerShellContext/Session/Host/EditorServicesPSHostUserInterface.cs index adb20b7ef..7cd740931 100644 --- a/src/PowerShellEditorServices/Services/PowerShellContext/Session/Host/EditorServicesPSHostUserInterface.cs +++ b/src/PowerShellEditorServices/Services/PowerShellContext/Session/Host/EditorServicesPSHostUserInterface.cs @@ -34,6 +34,8 @@ public abstract class EditorServicesPSHostUserInterface : private readonly ConcurrentDictionary currentProgressMessages = new ConcurrentDictionary(); + + private readonly bool _isPSReadLineEnabled; private PromptHandler activePromptHandler; private PSHostRawUserInterface rawUserInterface; private CancellationTokenSource commandLoopCancellationToken; @@ -104,11 +106,13 @@ public abstract class EditorServicesPSHostUserInterface : public EditorServicesPSHostUserInterface( PowerShellContextService powerShellContext, PSHostRawUserInterface rawUserInterface, + bool isPSReadLineEnabled, ILogger logger) { this.Logger = logger; this.powerShellContext = powerShellContext; this.rawUserInterface = rawUserInterface; + _isPSReadLineEnabled = isPSReadLineEnabled; this.powerShellContext.DebuggerStop += PowerShellContext_DebuggerStop; this.powerShellContext.DebuggerResumed += PowerShellContext_DebuggerResumed; @@ -850,7 +854,12 @@ private async Task StartReplLoopAsync(CancellationToken cancellationToken) } finally { - if (!cancellationToken.IsCancellationRequested && + // This supplies the newline in the Legacy ReadLine when executing code in the terminal via hitting the ENTER key. + // Without this, hitting ENTER with a no input looks like it does nothing (no new prompt is written) + // and also the output would show up on the same line as the code you wanted to execute (the prompt line). + // Since PSReadLine handles ENTER internally to itself, we only want to do this when using the Legacy ReadLine. + if (!_isPSReadLineEnabled && + !cancellationToken.IsCancellationRequested && originalCursorTop == await ConsoleProxy.GetCursorTopAsync(cancellationToken).ConfigureAwait(false)) { this.WriteLine(); diff --git a/src/PowerShellEditorServices/Services/PowerShellContext/Session/Host/ProtocolPSHostUserInterface.cs b/src/PowerShellEditorServices/Services/PowerShellContext/Session/Host/ProtocolPSHostUserInterface.cs index 6053245c2..32415922e 100644 --- a/src/PowerShellEditorServices/Services/PowerShellContext/Session/Host/ProtocolPSHostUserInterface.cs +++ b/src/PowerShellEditorServices/Services/PowerShellContext/Session/Host/ProtocolPSHostUserInterface.cs @@ -30,7 +30,11 @@ public ProtocolPSHostUserInterface( ILanguageServer languageServer, PowerShellContextService powerShellContext, ILogger logger) - : base(powerShellContext, new SimplePSHostRawUserInterface(logger), logger) + : base ( + powerShellContext, + new SimplePSHostRawUserInterface(logger), + isPSReadLineEnabled: false, + logger) { _languageServer = languageServer; } diff --git a/src/PowerShellEditorServices/Services/PowerShellContext/Session/Host/TerminalPSHostUserInterface.cs b/src/PowerShellEditorServices/Services/PowerShellContext/Session/Host/TerminalPSHostUserInterface.cs index 93e8173ad..ce6edee46 100644 --- a/src/PowerShellEditorServices/Services/PowerShellContext/Session/Host/TerminalPSHostUserInterface.cs +++ b/src/PowerShellEditorServices/Services/PowerShellContext/Session/Host/TerminalPSHostUserInterface.cs @@ -37,11 +37,13 @@ public class TerminalPSHostUserInterface : EditorServicesPSHostUserInterface /// The InternalHost instance from the origin runspace. public TerminalPSHostUserInterface( PowerShellContextService powerShellContext, - ILogger logger, - PSHost internalHost) - : base( + PSHost internalHost, + bool isPSReadLineEnabled, + ILogger logger) + : base ( powerShellContext, new TerminalPSHostRawUserInterface(logger, internalHost), + isPSReadLineEnabled, logger) { this.internalHostUI = internalHost.UI;