Skip to content

Commit 845d9dc

Browse files
Fix debugging output location (#1162)
* Fix debugging output location * gets rid of a rouge newline * update comment * don't use this.WriteOutput at all * Bring back finally for Legacy ReadLine * codacy * patrick feedback
1 parent 3b890c3 commit 845d9dc

File tree

4 files changed

+33
-17
lines changed

4 files changed

+33
-17
lines changed

src/PowerShellEditorServices/Services/PowerShellContext/PowerShellContextService.cs

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ public static PowerShellContextService Create(
199199

200200
EditorServicesPSHostUserInterface hostUserInterface =
201201
hostStartupInfo.ConsoleReplEnabled
202-
? (EditorServicesPSHostUserInterface)new TerminalPSHostUserInterface(powerShellContext, logger, hostStartupInfo.PSHost)
202+
? (EditorServicesPSHostUserInterface) new TerminalPSHostUserInterface(powerShellContext, hostStartupInfo.PSHost, shouldUsePSReadLine, logger)
203203
: new ProtocolPSHostUserInterface(languageServer, powerShellContext, logger);
204204

205205
EditorServicesPSHost psHost =
@@ -680,7 +680,9 @@ public async Task<IEnumerable<TResult>> ExecuteCommandAsync<TResult>(
680680
runspaceHandle = await this.GetRunspaceHandleAsync(executionOptions.IsReadLine).ConfigureAwait(false);
681681
if (executionOptions.WriteInputToHost)
682682
{
683-
this.WriteOutput(psCommand.Commands[0].CommandText, true);
683+
this.WriteOutput(
684+
psCommand.Commands[0].CommandText,
685+
includeNewLine: true);
684686
}
685687

686688
if (executionTarget == ExecutionTarget.Debugger)
@@ -1074,18 +1076,17 @@ public async Task ExecuteScriptWithArgsAsync(string script, string arguments = n
10741076
command.AddCommand(script, false);
10751077
}
10761078

1077-
if (writeInputToHost)
1078-
{
1079-
this.WriteOutput(
1080-
script + Environment.NewLine,
1081-
true);
1082-
}
10831079

10841080
await this.ExecuteCommandAsync<object>(
1085-
command,
1086-
errorMessages: null,
1087-
sendOutputToHost: true,
1088-
addToHistory: true).ConfigureAwait(false);
1081+
command,
1082+
errorMessages: null,
1083+
new ExecutionOptions
1084+
{
1085+
WriteInputToHost = true,
1086+
WriteOutputToHost = true,
1087+
WriteErrorsToHost = true,
1088+
AddToHistory = true,
1089+
}).ConfigureAwait(false);
10891090
}
10901091

10911092
/// <summary>

src/PowerShellEditorServices/Services/PowerShellContext/Session/Host/EditorServicesPSHostUserInterface.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ public abstract class EditorServicesPSHostUserInterface :
3434

3535
private readonly ConcurrentDictionary<ProgressKey, object> currentProgressMessages =
3636
new ConcurrentDictionary<ProgressKey, object>();
37+
38+
private readonly bool _isPSReadLineEnabled;
3739
private PromptHandler activePromptHandler;
3840
private PSHostRawUserInterface rawUserInterface;
3941
private CancellationTokenSource commandLoopCancellationToken;
@@ -104,11 +106,13 @@ public abstract class EditorServicesPSHostUserInterface :
104106
public EditorServicesPSHostUserInterface(
105107
PowerShellContextService powerShellContext,
106108
PSHostRawUserInterface rawUserInterface,
109+
bool isPSReadLineEnabled,
107110
ILogger logger)
108111
{
109112
this.Logger = logger;
110113
this.powerShellContext = powerShellContext;
111114
this.rawUserInterface = rawUserInterface;
115+
_isPSReadLineEnabled = isPSReadLineEnabled;
112116

113117
this.powerShellContext.DebuggerStop += PowerShellContext_DebuggerStop;
114118
this.powerShellContext.DebuggerResumed += PowerShellContext_DebuggerResumed;
@@ -850,7 +854,12 @@ private async Task StartReplLoopAsync(CancellationToken cancellationToken)
850854
}
851855
finally
852856
{
853-
if (!cancellationToken.IsCancellationRequested &&
857+
// This supplies the newline in the Legacy ReadLine when executing code in the terminal via hitting the ENTER key.
858+
// Without this, hitting ENTER with a no input looks like it does nothing (no new prompt is written)
859+
// and also the output would show up on the same line as the code you wanted to execute (the prompt line).
860+
// Since PSReadLine handles ENTER internally to itself, we only want to do this when using the Legacy ReadLine.
861+
if (!_isPSReadLineEnabled &&
862+
!cancellationToken.IsCancellationRequested &&
854863
originalCursorTop == await ConsoleProxy.GetCursorTopAsync(cancellationToken).ConfigureAwait(false))
855864
{
856865
this.WriteLine();

src/PowerShellEditorServices/Services/PowerShellContext/Session/Host/ProtocolPSHostUserInterface.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,11 @@ public ProtocolPSHostUserInterface(
3030
ILanguageServer languageServer,
3131
PowerShellContextService powerShellContext,
3232
ILogger logger)
33-
: base(powerShellContext, new SimplePSHostRawUserInterface(logger), logger)
33+
: base (
34+
powerShellContext,
35+
new SimplePSHostRawUserInterface(logger),
36+
isPSReadLineEnabled: false,
37+
logger)
3438
{
3539
_languageServer = languageServer;
3640
}

src/PowerShellEditorServices/Services/PowerShellContext/Session/Host/TerminalPSHostUserInterface.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,13 @@ public class TerminalPSHostUserInterface : EditorServicesPSHostUserInterface
3737
/// <param name="internalHost">The InternalHost instance from the origin runspace.</param>
3838
public TerminalPSHostUserInterface(
3939
PowerShellContextService powerShellContext,
40-
ILogger logger,
41-
PSHost internalHost)
42-
: base(
40+
PSHost internalHost,
41+
bool isPSReadLineEnabled,
42+
ILogger logger)
43+
: base (
4344
powerShellContext,
4445
new TerminalPSHostRawUserInterface(logger, internalHost),
46+
isPSReadLineEnabled,
4547
logger)
4648
{
4749
this.internalHostUI = internalHost.UI;

0 commit comments

Comments
 (0)