Skip to content

Commit 6f9b20c

Browse files
committed
Slight tweaks to ConsoleService APIs
1 parent 5dd00e9 commit 6f9b20c

File tree

3 files changed

+25
-26
lines changed

3 files changed

+25
-26
lines changed

src/PowerShellEditorServices.Protocol/Server/LanguageServer.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ protected override void Initialize()
146146
protected override async Task Shutdown()
147147
{
148148
// Stop the interactive terminal
149-
this.editorSession.ConsoleService.StopInteractiveConsole();
149+
this.editorSession.ConsoleService.CancelReadLoop();
150150

151151
// Make sure remaining output is flushed before exiting
152152
await this.outputDebouncer.Flush();
@@ -1080,6 +1080,9 @@ protected Task HandleEvaluateRequest(
10801080
DebugAdapterMessages.EvaluateRequestArguments evaluateParams,
10811081
RequestContext<DebugAdapterMessages.EvaluateResponseBody> requestContext)
10821082
{
1083+
// Cancel the read loop before executing
1084+
this.editorSession.ConsoleService.CancelReadLoop();
1085+
10831086
// We don't await the result of the execution here because we want
10841087
// to be able to receive further messages while the current script
10851088
// is executing. This important in cases where the pipeline thread
@@ -1095,6 +1098,9 @@ protected Task HandleEvaluateRequest(
10951098
executeTask.ContinueWith(
10961099
(task) =>
10971100
{
1101+
// Start the command loop again
1102+
this.editorSession.ConsoleService.StartReadLoop();
1103+
10981104
// Return an empty result since the result value is irrelevant
10991105
// for this request in the LanguageServer
11001106
return

src/PowerShellEditorServices/Console/ConsoleService.cs

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -80,18 +80,6 @@ public ConsoleService(
8080

8181
this.consoleReadLine = new ConsoleReadLine(powerShellContext);
8282

83-
// TODO: Move this to the PSHost implementation
84-
85-
Console.CancelKeyPress +=
86-
(obj, args) =>
87-
{
88-
// TODO: Don't abort if we're in a native app, let the native app
89-
// handle the ctrl+c
90-
91-
// We'll handle Ctrl+C
92-
args.Cancel = true;
93-
this.powerShellContext.AbortExecution();
94-
};
9583
}
9684

9785
#endregion
@@ -138,18 +126,6 @@ public void CancelReadLoop()
138126
}
139127
}
140128

141-
/// <summary>
142-
/// Stops the current terminal-based interactive console.
143-
/// </summary>
144-
public void StopInteractiveConsole()
145-
{
146-
if (this.readLineCancellationToken != null)
147-
{
148-
this.readLineCancellationToken.Cancel();
149-
this.readLineCancellationToken = null;
150-
}
151-
}
152-
153129
/// <summary>
154130
/// Called when a command string is received from the user.
155131
/// If a prompt is currently active, the prompt handler is
@@ -321,7 +297,7 @@ await this.consoleReadLine.ReadCommandLine(
321297
catch (Exception e) // Narrow this if possible
322298
{
323299
this.WriteOutput(
324-
$"\n\nAn error occurred while accepting input:\n\n{e.ToString()}\n",
300+
$"\n\nAn error occurred while reading input:\n\n{e.ToString()}\n",
325301
true,
326302
OutputType.Error);
327303

src/PowerShellEditorServices/Session/SessionPSHost.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ internal class ConsoleServicePSHost : PSHost, IHostSupportsInteractiveSession
2323

2424
private HostDetails hostDetails;
2525
private IConsoleHost consoleHost;
26+
private bool isNativeApplicationRunning;
2627
private Guid instanceId = Guid.NewGuid();
2728
private ConsoleServicePSHostUserInterface hostUserInterface;
2829
private IHostSupportsInteractiveSession hostSupportsInteractiveSession;
@@ -62,6 +63,20 @@ public ConsoleServicePSHost(
6263
this.hostDetails = hostDetails;
6364
this.hostUserInterface = new ConsoleServicePSHostUserInterface();
6465
this.hostSupportsInteractiveSession = hostSupportsInteractiveSession;
66+
67+
System.Console.CancelKeyPress +=
68+
(obj, args) =>
69+
{
70+
if (!this.isNativeApplicationRunning)
71+
{
72+
// We'll handle Ctrl+C
73+
if (this.ConsoleHost != null)
74+
{
75+
args.Cancel = true;
76+
this.consoleHost.SendControlC();
77+
}
78+
}
79+
};
6580
}
6681

6782
#endregion
@@ -113,11 +128,13 @@ public override void ExitNestedPrompt()
113128
public override void NotifyBeginApplication()
114129
{
115130
Logger.Write(LogLevel.Verbose, "NotifyBeginApplication() called.");
131+
this.isNativeApplicationRunning = true;
116132
}
117133

118134
public override void NotifyEndApplication()
119135
{
120136
Logger.Write(LogLevel.Verbose, "NotifyEndApplication() called.");
137+
this.isNativeApplicationRunning = false;
121138
}
122139

123140
public override void SetShouldExit(int exitCode)

0 commit comments

Comments
 (0)