Skip to content

Commit a1bc248

Browse files
committed
Establish new host interface abstraction model, remove ConsoleService
This change is a fairly large refactoring of our PSHost implementation to remove the ConsoleService and IConsoleHost types and instead use two different PSHostUserInterface implementations. Here's a detailed breakdown of the changes: - Centralized host UI behavior in EditorServicesPSHostUserInterface abstract class. This class now exposes overridable abstract methods that simplify PSHostUserInterface implementations that will be used with the EditorServicesPSHost. - Removed the ConsoleService class and put its behavior and abstractions into EditorServicesPSHostUserInterface. - Created TerminalPSHostUserInterface for the integrated terminal host experience. - Created ProtocolPSHostUserInterface for the protocol-based host experience. - Removed the concept of a "prompt handler context" because each host implementation will have a single way to deal with input and choice prompts. - Lifted direct management of the console interface out of the LanguageServer and DebugAdapter classes, now managed in the EditorServicesHost. - Disabled the ConsoleServiceTests until we decide how to test for this behavior at the level of the EditorServicesPSHostUserInterface.
1 parent 59edcb0 commit a1bc248

28 files changed

+2117
-2120
lines changed

src/PowerShellEditorServices.Host/EditorServicesHost.cs

Lines changed: 34 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//
1+
//
22
// Copyright (c) Microsoft. All rights reserved.
33
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
44
//
@@ -195,6 +195,8 @@ private async void OnLanguageServiceClientConnect(
195195
CreateSession(
196196
this.hostDetails,
197197
this.profilePaths,
198+
protocolEndpoint,
199+
messageDispatcher,
198200
this.enableConsoleRepl);
199201

200202
this.languageServer =
@@ -264,7 +266,10 @@ private void OnDebugServiceClientConnect(object sender, TcpSocketServerChannel s
264266
this.CreateDebugSession(
265267
this.hostDetails,
266268
profilePaths,
267-
this.languageServer?.EditorOperations);
269+
protocolEndpoint,
270+
messageDispatcher,
271+
this.languageServer?.EditorOperations,
272+
false);
268273

269274
this.debugAdapter =
270275
new DebugAdapter(
@@ -318,47 +323,61 @@ public void WaitForCompletion()
318323
private EditorSession CreateSession(
319324
HostDetails hostDetails,
320325
ProfilePaths profilePaths,
326+
IMessageSender messageSender,
327+
IMessageHandlers messageHandlers,
321328
bool enableConsoleRepl)
322329
{
323330
EditorSession editorSession = new EditorSession(this.logger);
324331
PowerShellContext powerShellContext = new PowerShellContext(this.logger);
325332

326-
ConsoleServicePSHost psHost =
327-
new ConsoleServicePSHost(
333+
EditorServicesPSHostUserInterface hostUserInterface =
334+
enableConsoleRepl
335+
? (EditorServicesPSHostUserInterface) new TerminalPSHostUserInterface(powerShellContext, this.logger)
336+
: new ProtocolPSHostUserInterface(powerShellContext, messageSender, messageHandlers, this.logger);
337+
338+
EditorServicesPSHost psHost =
339+
new EditorServicesPSHost(
328340
powerShellContext,
329341
hostDetails,
330-
enableConsoleRepl);
342+
hostUserInterface,
343+
this.logger);
331344

332345
Runspace initialRunspace = PowerShellContext.CreateRunspace(psHost);
333-
powerShellContext.Initialize(profilePaths, initialRunspace, true, psHost.ConsoleService);
346+
powerShellContext.Initialize(profilePaths, initialRunspace, true, hostUserInterface);
334347

335-
editorSession.StartSession(
336-
powerShellContext,
337-
psHost.ConsoleService);
348+
editorSession.StartSession(powerShellContext, hostUserInterface);
338349

339350
return editorSession;
340351
}
341352

342353
private EditorSession CreateDebugSession(
343354
HostDetails hostDetails,
344355
ProfilePaths profilePaths,
345-
IEditorOperations editorOperations)
356+
IMessageSender messageSender,
357+
IMessageHandlers messageHandlers,
358+
IEditorOperations editorOperations,
359+
bool enableConsoleRepl)
346360
{
347361
EditorSession editorSession = new EditorSession(this.logger);
348362
PowerShellContext powerShellContext = new PowerShellContext(this.logger);
349363

350-
ConsoleServicePSHost psHost =
351-
new ConsoleServicePSHost(
364+
EditorServicesPSHostUserInterface hostUserInterface =
365+
enableConsoleRepl
366+
? (EditorServicesPSHostUserInterface) new TerminalPSHostUserInterface(powerShellContext, this.logger)
367+
: new ProtocolPSHostUserInterface(powerShellContext, messageSender, messageHandlers, this.logger);
368+
369+
EditorServicesPSHost psHost =
370+
new EditorServicesPSHost(
352371
powerShellContext,
353372
hostDetails,
354-
enableConsoleRepl);
373+
hostUserInterface,
374+
this.logger);
355375

356376
Runspace initialRunspace = PowerShellContext.CreateRunspace(psHost);
357-
powerShellContext.Initialize(profilePaths, initialRunspace, true, psHost.ConsoleService);
377+
powerShellContext.Initialize(profilePaths, initialRunspace, true, hostUserInterface);
358378

359379
editorSession.StartDebugSession(
360380
powerShellContext,
361-
psHost.ConsoleService,
362381
editorOperations);
363382

364383
return editorSession;

src/PowerShellEditorServices.Protocol/Server/PromptHandlers.cs renamed to src/PowerShellEditorServices.Host/PSHost/PromptHandlers.cs

Lines changed: 26 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -12,51 +12,24 @@
1212
using System.Threading;
1313
using System.Security;
1414

15-
namespace Microsoft.PowerShell.EditorServices.Protocol.Server
15+
namespace Microsoft.PowerShell.EditorServices.Host
1616
{
17-
internal class ProtocolPromptHandlerContext : IPromptHandlerContext
18-
{
19-
private IMessageSender messageSender;
20-
private ConsoleService consoleService;
21-
22-
public ProtocolPromptHandlerContext(
23-
IMessageSender messageSender,
24-
ConsoleService consoleService)
25-
{
26-
this.messageSender = messageSender;
27-
this.consoleService = consoleService;
28-
}
29-
30-
public ChoicePromptHandler GetChoicePromptHandler()
31-
{
32-
return new ProtocolChoicePromptHandler(
33-
this.messageSender,
34-
this.consoleService,
35-
Logger.CurrentLogger);
36-
}
37-
38-
public InputPromptHandler GetInputPromptHandler()
39-
{
40-
return new ProtocolInputPromptHandler(
41-
this.messageSender,
42-
this.consoleService);
43-
}
44-
}
45-
4617
internal class ProtocolChoicePromptHandler : ConsoleChoicePromptHandler
4718
{
19+
private IHostInput hostInput;
4820
private IMessageSender messageSender;
49-
private ConsoleService consoleService;
5021
private TaskCompletionSource<string> readLineTask;
5122

5223
public ProtocolChoicePromptHandler(
5324
IMessageSender messageSender,
54-
ConsoleService consoleService,
25+
IHostInput hostInput,
26+
IHostOutput hostOutput,
5527
ILogger logger)
56-
: base(consoleService, logger)
28+
: base(hostOutput, logger)
5729
{
30+
this.hostInput = hostInput;
31+
this.hostOutput = hostOutput;
5832
this.messageSender = messageSender;
59-
this.consoleService = consoleService;
6033
}
6134

6235
protected override void ShowPrompt(PromptStyle promptStyle)
@@ -93,7 +66,7 @@ private void HandlePromptResponse(
9366

9467
if (!response.PromptCancelled)
9568
{
96-
this.consoleService.WriteOutput(
69+
this.hostOutput.WriteOutput(
9770
response.ResponseText,
9871
OutputType.Normal);
9972

@@ -102,7 +75,7 @@ private void HandlePromptResponse(
10275
else
10376
{
10477
// Cancel the current prompt
105-
this.consoleService.SendControlC();
78+
this.hostInput.SendControlC();
10679
}
10780
}
10881
else
@@ -117,7 +90,7 @@ private void HandlePromptResponse(
11790
}
11891

11992
// Cancel the current prompt
120-
this.consoleService.SendControlC();
93+
this.hostInput.SendControlC();
12194
}
12295

12396
this.readLineTask = null;
@@ -126,37 +99,24 @@ private void HandlePromptResponse(
12699

127100
internal class ProtocolInputPromptHandler : ConsoleInputPromptHandler
128101
{
102+
private IHostInput hostInput;
129103
private IMessageSender messageSender;
130-
private ConsoleService consoleService;
131104
private TaskCompletionSource<string> readLineTask;
132105

133106
public ProtocolInputPromptHandler(
134107
IMessageSender messageSender,
135-
ConsoleService consoleService)
136-
: base(
137-
consoleService,
138-
Microsoft.PowerShell.EditorServices.Utility.Logger.CurrentLogger)
108+
IHostInput hostInput,
109+
IHostOutput hostOutput,
110+
ILogger logger)
111+
: base(hostOutput, logger)
139112
{
113+
this.hostInput = hostInput;
114+
this.hostOutput = hostOutput;
140115
this.messageSender = messageSender;
141-
this.consoleService = consoleService;
142-
}
143-
144-
protected override void ShowErrorMessage(Exception e)
145-
{
146-
// Use default behavior for writing the error message
147-
base.ShowErrorMessage(e);
148-
}
149-
150-
protected override void ShowPromptMessage(string caption, string message)
151-
{
152-
// Use default behavior for writing the prompt message
153-
base.ShowPromptMessage(caption, message);
154116
}
155117

156118
protected override void ShowFieldPrompt(FieldDetails fieldDetails)
157119
{
158-
// Write the prompt to the console first so that there's a record
159-
// of it occurring
160120
base.ShowFieldPrompt(fieldDetails);
161121

162122
messageSender
@@ -186,7 +146,7 @@ private void HandlePromptResponse(
186146

187147
if (!response.PromptCancelled)
188148
{
189-
this.consoleService.WriteOutput(
149+
this.hostOutput.WriteOutput(
190150
response.ResponseText,
191151
OutputType.Normal);
192152

@@ -195,7 +155,7 @@ private void HandlePromptResponse(
195155
else
196156
{
197157
// Cancel the current prompt
198-
this.consoleService.SendControlC();
158+
this.hostInput.SendControlC();
199159
}
200160
}
201161
else
@@ -210,11 +170,16 @@ private void HandlePromptResponse(
210170
}
211171

212172
// Cancel the current prompt
213-
this.consoleService.SendControlC();
173+
this.hostInput.SendControlC();
214174
}
215175

216176
this.readLineTask = null;
217177
}
178+
179+
protected override Task<SecureString> ReadSecureString(CancellationToken cancellationToken)
180+
{
181+
// TODO: Write a message to the console
182+
throw new NotImplementedException();
183+
}
218184
}
219185
}
220-

0 commit comments

Comments
 (0)