Skip to content

Commit 6df8d6d

Browse files
committed
Move EditorSession creation out of LanguageServer and DebugAdapter
This change is the first step toward moving runspace and feature creation to the host's entry point so that it can be more configurable. The creation of EditorSession is now handled by the EditorServicesHost. This is the start of further refactoring so expect more changes.
1 parent ffbf999 commit 6df8d6d

File tree

4 files changed

+63
-60
lines changed

4 files changed

+63
-60
lines changed

src/PowerShellEditorServices.Host/EditorServicesHost.cs

Lines changed: 43 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
using System.Diagnostics;
1313
using System.Reflection;
1414
using System.Threading;
15+
using Microsoft.PowerShell.EditorServices.Extensions;
1516

1617
namespace Microsoft.PowerShell.EditorServices.Host
1718
{
@@ -34,6 +35,7 @@ public class EditorServicesHost
3435
private HostDetails hostDetails;
3536
private string bundledModulesPath;
3637
private DebugAdapter debugAdapter;
38+
private EditorSession editorSession;
3739
private HashSet<string> featureFlags;
3840
private LanguageServer languageServer;
3941

@@ -82,7 +84,7 @@ public EditorServicesHost(
8284
else
8385
{
8486
Debugger.Launch();
85-
}
87+
}
8688
}
8789
#endif
8890

@@ -148,11 +150,15 @@ public void StartLogging(string logFilePath, LogLevel logLevel)
148150
/// <param name="profilePaths">The object containing the profile paths to load for this session.</param>
149151
public void StartLanguageService(int languageServicePort, ProfilePaths profilePaths)
150152
{
153+
this.editorSession =
154+
CreateSession(
155+
this.hostDetails,
156+
profilePaths,
157+
this.enableConsoleRepl);
158+
151159
this.languageServer =
152160
new LanguageServer(
153-
hostDetails,
154-
profilePaths,
155-
this.enableConsoleRepl,
161+
this.editorSession,
156162
new TcpSocketServerChannel(languageServicePort));
157163

158164
this.languageServer.Start().Wait();
@@ -177,17 +183,23 @@ public void StartDebugService(
177183
{
178184
this.debugAdapter =
179185
new DebugAdapter(
180-
this.languageServer.EditorSession,
181-
new TcpSocketServerChannel(debugServicePort));
186+
this.editorSession,
187+
new TcpSocketServerChannel(debugServicePort),
188+
false);
182189
}
183190
else
184191
{
192+
EditorSession debugSession =
193+
this.CreateDebugSession(
194+
this.hostDetails,
195+
profilePaths,
196+
this.languageServer.EditorOperations);
197+
185198
this.debugAdapter =
186199
new DebugAdapter(
187-
hostDetails,
188-
profilePaths,
200+
debugSession,
189201
new TcpSocketServerChannel(debugServicePort),
190-
this.languageServer?.EditorOperations);
202+
true);
191203
}
192204

193205
this.debugAdapter.SessionEnded +=
@@ -253,6 +265,28 @@ public void WaitForCompletion()
253265

254266
#region Private Methods
255267

268+
private EditorSession CreateSession(
269+
HostDetails hostDetails,
270+
ProfilePaths profilePaths,
271+
bool enableConsoleRepl)
272+
{
273+
EditorSession editorSession = new EditorSession();
274+
editorSession.StartSession(hostDetails, profilePaths, enableConsoleRepl);
275+
276+
return editorSession;
277+
}
278+
279+
private EditorSession CreateDebugSession(
280+
HostDetails hostDetails,
281+
ProfilePaths profilePaths,
282+
IEditorOperations editorOperations)
283+
{
284+
EditorSession editorSession = new EditorSession();
285+
editorSession.StartDebugSession(hostDetails, profilePaths, editorOperations);
286+
287+
return editorSession;
288+
}
289+
256290
#if !CoreCLR
257291
static void CurrentDomain_UnhandledException(
258292
object sender,

src/PowerShellEditorServices.Protocol/Server/DebugAdapter.cs

Lines changed: 7 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -37,40 +37,17 @@ public class DebugAdapter : DebugAdapterBase
3737
private bool isInteractiveDebugSession;
3838
private RequestContext<object> disconnectRequestContext = null;
3939

40-
public DebugAdapter(HostDetails hostDetails, ProfilePaths profilePaths)
41-
: this(hostDetails, profilePaths, new StdioServerChannel(), null)
42-
{
43-
}
44-
45-
public DebugAdapter(EditorSession editorSession, ChannelBase serverChannel)
46-
: base(serverChannel)
47-
{
48-
this.editorSession = editorSession;
49-
}
50-
5140
public DebugAdapter(
52-
HostDetails hostDetails,
53-
ProfilePaths profilePaths,
41+
EditorSession editorSession,
5442
ChannelBase serverChannel,
55-
IEditorOperations editorOperations)
56-
: this(hostDetails, profilePaths, serverChannel, editorOperations, false)
43+
bool ownsEditorSession)
44+
: base(serverChannel)
5745
{
46+
this.editorSession = editorSession;
47+
this.ownsEditorSession = ownsEditorSession;
48+
this.enableConsoleRepl = editorSession.UsesConsoleHost;
5849
}
5950

60-
public DebugAdapter(
61-
HostDetails hostDetails,
62-
ProfilePaths profilePaths,
63-
ChannelBase serverChannel,
64-
IEditorOperations editorOperations,
65-
bool enableConsoleRepl)
66-
: base(serverChannel)
67-
{
68-
this.ownsEditorSession = true;
69-
this.editorSession = new EditorSession();
70-
this.editorSession.StartDebugSession(hostDetails, profilePaths, editorOperations);
71-
this.enableConsoleRepl = enableConsoleRepl;
72-
}
73-
7451
protected override void Initialize()
7552
{
7653
// Register all supported message types
@@ -325,6 +302,7 @@ protected async Task HandleLaunchRequest(
325302

326303
if (this.editorSession.ConsoleService.EnableConsoleRepl)
327304
{
305+
// TODO: Write this during DebugSession init
328306
await this.WriteUseIntegratedConsoleMessage();
329307
}
330308

src/PowerShellEditorServices.Protocol/Server/LanguageServer.cs

Lines changed: 6 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -45,31 +45,15 @@ public IEditorOperations EditorOperations
4545
get { return this.editorOperations; }
4646
}
4747

48-
public EditorSession EditorSession
49-
{
50-
get { return this.editorSession; }
51-
}
52-
53-
/// <param name="hostDetails">
54-
/// Provides details about the host application.
55-
/// </param>
56-
public LanguageServer(HostDetails hostDetails, ProfilePaths profilePaths)
57-
: this(hostDetails, profilePaths, false, new StdioServerChannel())
58-
{
59-
}
60-
6148
/// <param name="hostDetails">
6249
/// Provides details about the host application.
6350
/// </param>
6451
public LanguageServer(
65-
HostDetails hostDetails,
66-
ProfilePaths profilePaths,
67-
bool enableConsoleRepl,
52+
EditorSession editorSession,
6853
ChannelBase serverChannel)
6954
: base(serverChannel)
7055
{
71-
this.editorSession = new EditorSession();
72-
this.editorSession.StartSession(hostDetails, profilePaths, enableConsoleRepl);
56+
this.editorSession = editorSession;
7357
this.editorSession.PowerShellContext.RunspaceChanged += PowerShellContext_RunspaceChanged;
7458

7559
// Attach to ExtensionService events
@@ -86,7 +70,7 @@ public LanguageServer(
8670
this.editorSession.StartDebugService(this.editorOperations);
8771
this.editorSession.DebugService.DebuggerStopped += DebugService_DebuggerStopped;
8872

89-
if (enableConsoleRepl)
73+
if (this.editorSession.UsesConsoleHost)
9074
{
9175
this.editorSession.ConsoleService.EnableConsoleRepl = true;
9276
}
@@ -1080,18 +1064,18 @@ protected async Task HandleCommentHelpRequest(
10801064
CommentHelpRequestParams requestParams,
10811065
RequestContext<CommentHelpRequestResult> requestContext)
10821066
{
1083-
var scriptFile = EditorSession.Workspace.GetFile(requestParams.DocumentUri);
1067+
var scriptFile = this.editorSession.Workspace.GetFile(requestParams.DocumentUri);
10841068
var expectedFunctionLine = requestParams.TriggerPosition.Line + 2;
10851069

10861070
string helpLocation;
1087-
var functionDefinitionAst = EditorSession.LanguageService.GetFunctionDefinitionForHelpComment(
1071+
var functionDefinitionAst = this.editorSession.LanguageService.GetFunctionDefinitionForHelpComment(
10881072
scriptFile,
10891073
requestParams.TriggerPosition.Line + 1,
10901074
out helpLocation);
10911075
var result = new CommentHelpRequestResult();
10921076
if (functionDefinitionAst != null)
10931077
{
1094-
var analysisResults = await EditorSession.AnalysisService.GetSemanticMarkersAsync(
1078+
var analysisResults = await this.editorSession.AnalysisService.GetSemanticMarkersAsync(
10951079
functionDefinitionAst.Extent.Text,
10961080
AnalysisService.GetCommentHelpRuleSettings(
10971081
true,

src/PowerShellEditorServices/Session/EditorSession.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,12 @@ public class EditorSession
6565
/// </summary>
6666
public RemoteFileManager RemoteFileManager { get; private set; }
6767

68+
/// <summary>
69+
/// Gets a boolean which is true if the integrated console host is
70+
/// active in this session.
71+
/// </summary>
72+
public bool UsesConsoleHost { get; private set; }
73+
6874
#endregion
6975

7076
#region Public Methods
@@ -93,6 +99,7 @@ public void StartSession(
9399
this.ConsoleService = new ConsoleService(this.PowerShellContext);
94100
this.ExtensionService = new ExtensionService(this.PowerShellContext);
95101
this.TemplateService = new TemplateService(this.PowerShellContext);
102+
this.UsesConsoleHost = enableConsoleRepl;
96103

97104
this.InstantiateAnalysisService();
98105

0 commit comments

Comments
 (0)