Skip to content

Commit 0e2f7b9

Browse files
refactor server setup (PowerShell#1018)
1 parent a2a986e commit 0e2f7b9

10 files changed

+433
-409
lines changed

src/PowerShellEditorServices.Engine/Hosting/EditorServicesHost.cs

+26-111
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,15 @@
66
using System;
77
using System.Collections.Generic;
88
using System.Diagnostics;
9-
using System.IO;
109
using System.Linq;
1110
using System.Management.Automation;
1211
using System.Management.Automation.Host;
13-
using System.Management.Automation.Runspaces;
1412
using System.Reflection;
1513
using System.Runtime.InteropServices;
1614
using System.Threading;
1715
using System.Threading.Tasks;
18-
using Microsoft.Extensions.DependencyInjection;
1916
using Microsoft.Extensions.Logging;
20-
using Microsoft.PowerShell.EditorServices.Extensions;
21-
using Microsoft.PowerShell.EditorServices.Host;
22-
using Microsoft.PowerShell.EditorServices.Templates;
17+
using Microsoft.PowerShell.EditorServices.Engine.Server;
2318
using Serilog;
2419

2520
namespace Microsoft.PowerShell.EditorServices.Engine
@@ -62,8 +57,6 @@ public class EditorServicesHost
6257
{
6358
#region Private Fields
6459

65-
private readonly IServiceCollection _serviceCollection;
66-
6760
private readonly HostDetails _hostDetails;
6861

6962
private readonly PSHost _internalHost;
@@ -74,7 +67,7 @@ public class EditorServicesHost
7467

7568
private readonly string[] _additionalModules;
7669

77-
private ILanguageServer _languageServer;
70+
private PsesLanguageServer _languageServer;
7871

7972
private Microsoft.Extensions.Logging.ILogger _logger;
8073

@@ -139,16 +132,15 @@ public EditorServicesHost(
139132
Validate.IsNotNull(nameof(hostDetails), hostDetails);
140133
Validate.IsNotNull(nameof(internalHost), internalHost);
141134

142-
_serviceCollection = new ServiceCollection();
143135
_hostDetails = hostDetails;
144136

145137
//this._hostDetails = hostDetails;
146-
this._enableConsoleRepl = enableConsoleRepl;
138+
_enableConsoleRepl = enableConsoleRepl;
147139
//this.bundledModulesPath = bundledModulesPath;
148-
this._additionalModules = additionalModules ?? Array.Empty<string>();
149-
this._featureFlags = new HashSet<string>(featureFlags ?? Array.Empty<string>());
140+
_additionalModules = additionalModules ?? Array.Empty<string>();
141+
_featureFlags = new HashSet<string>(featureFlags ?? Array.Empty<string>());
150142
//this.serverCompletedTask = new TaskCompletionSource<bool>();
151-
this._internalHost = internalHost;
143+
_internalHost = internalHost;
152144

153145
#if DEBUG
154146
if (waitForDebugger)
@@ -236,59 +228,33 @@ public void StartLanguageService(
236228

237229
_logger.LogInformation($"LSP NamedPipe: {config.InOutPipeName}\nLSP OutPipe: {config.OutPipeName}");
238230

239-
_serviceCollection
240-
.AddSingleton<WorkspaceService>()
241-
.AddSingleton<SymbolsService>()
242-
.AddSingleton<ConfigurationService>()
243-
.AddSingleton<PowerShellContextService>(
244-
(provider) =>
245-
GetFullyInitializedPowerShellContext(
246-
provider.GetService<OmniSharp.Extensions.LanguageServer.Protocol.Server.ILanguageServer>(),
247-
profilePaths))
248-
.AddSingleton<TemplateService>()
249-
.AddSingleton<EditorOperationsService>()
250-
.AddSingleton<ExtensionService>(
251-
(provider) =>
252-
{
253-
var extensionService = new ExtensionService(
254-
provider.GetService<PowerShellContextService>(),
255-
provider.GetService<OmniSharp.Extensions.LanguageServer.Protocol.Server.ILanguageServer>());
256-
extensionService.InitializeAsync(
257-
serviceProvider: provider,
258-
editorOperations: provider.GetService<EditorOperationsService>())
259-
.Wait();
260-
return extensionService;
261-
})
262-
.AddSingleton<AnalysisService>(
263-
(provider) =>
264-
{
265-
return AnalysisService.Create(
266-
provider.GetService<ConfigurationService>(),
267-
provider.GetService<OmniSharp.Extensions.LanguageServer.Protocol.Server.ILanguageServer>(),
268-
_factory.CreateLogger<AnalysisService>());
269-
});
231+
270232

271233
switch (config.TransportType)
272234
{
273235
case EditorServiceTransportType.NamedPipe:
274-
_languageServer = new OmnisharpLanguageServerBuilder(_serviceCollection)
275-
{
276-
NamedPipeName = config.InOutPipeName ?? config.InPipeName,
277-
OutNamedPipeName = config.OutPipeName,
278-
LoggerFactory = _factory,
279-
MinimumLogLevel = LogLevel.Trace,
280-
}
281-
.BuildLanguageServer();
236+
_languageServer = new NamedPipePsesLanguageServer(
237+
_factory,
238+
LogLevel.Trace,
239+
_enableConsoleRepl,
240+
_featureFlags,
241+
_hostDetails,
242+
_additionalModules,
243+
_internalHost,
244+
profilePaths,
245+
config.InOutPipeName ?? config.InPipeName,
246+
config.OutPipeName);
282247
break;
283248

284249
case EditorServiceTransportType.Stdio:
285-
_languageServer = new OmnisharpLanguageServerBuilder(_serviceCollection)
286-
{
287-
Stdio = true,
288-
LoggerFactory = _factory,
289-
MinimumLogLevel = LogLevel.Trace,
290-
}
291-
.BuildLanguageServer();
250+
_languageServer = new StdioPsesLanguageServer(
251+
_factory,
252+
LogLevel.Trace,
253+
_featureFlags,
254+
_hostDetails,
255+
_additionalModules,
256+
_internalHost,
257+
profilePaths);
292258
break;
293259
}
294260

@@ -302,57 +268,6 @@ public void StartLanguageService(
302268
config.TransportType, config.Endpoint));
303269
}
304270

305-
private PowerShellContextService GetFullyInitializedPowerShellContext(
306-
OmniSharp.Extensions.LanguageServer.Protocol.Server.ILanguageServer languageServer,
307-
ProfilePaths profilePaths)
308-
{
309-
var logger = _factory.CreateLogger<PowerShellContextService>();
310-
311-
// PSReadLine can only be used when -EnableConsoleRepl is specified otherwise
312-
// issues arise when redirecting stdio.
313-
var powerShellContext = new PowerShellContextService(
314-
logger,
315-
languageServer,
316-
_featureFlags.Contains("PSReadLine") && _enableConsoleRepl);
317-
318-
EditorServicesPSHostUserInterface hostUserInterface =
319-
_enableConsoleRepl
320-
? (EditorServicesPSHostUserInterface) new TerminalPSHostUserInterface(powerShellContext, logger, _internalHost)
321-
: new ProtocolPSHostUserInterface(languageServer, powerShellContext, logger);
322-
323-
EditorServicesPSHost psHost =
324-
new EditorServicesPSHost(
325-
powerShellContext,
326-
_hostDetails,
327-
hostUserInterface,
328-
logger);
329-
330-
Runspace initialRunspace = PowerShellContextService.CreateRunspace(psHost);
331-
powerShellContext.Initialize(profilePaths, initialRunspace, true, hostUserInterface);
332-
333-
powerShellContext.ImportCommandsModuleAsync(
334-
Path.Combine(
335-
Path.GetDirectoryName(this.GetType().GetTypeInfo().Assembly.Location),
336-
@"..\Commands"));
337-
338-
// TODO: This can be moved to the point after the $psEditor object
339-
// gets initialized when that is done earlier than LanguageServer.Initialize
340-
foreach (string module in this._additionalModules)
341-
{
342-
var command =
343-
new PSCommand()
344-
.AddCommand("Microsoft.PowerShell.Core\\Import-Module")
345-
.AddParameter("Name", module);
346-
347-
powerShellContext.ExecuteCommandAsync<PSObject>(
348-
command,
349-
sendOutputToHost: false,
350-
sendErrorToHost: true);
351-
}
352-
353-
return powerShellContext;
354-
}
355-
356271
/// <summary>
357272
/// Starts the debug service with the specified config.
358273
/// </summary>

src/PowerShellEditorServices.Engine/Interface/ILanguageServer.cs

-11
This file was deleted.

src/PowerShellEditorServices.Engine/Interface/ILanguageServerBuilder.cs

-17
This file was deleted.

0 commit comments

Comments
 (0)