Skip to content

Refactor server setup #1018

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
137 changes: 26 additions & 111 deletions src/PowerShellEditorServices.Engine/Hosting/EditorServicesHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,15 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Management.Automation;
using System.Management.Automation.Host;
using System.Management.Automation.Runspaces;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.PowerShell.EditorServices.Extensions;
using Microsoft.PowerShell.EditorServices.Host;
using Microsoft.PowerShell.EditorServices.Templates;
using Microsoft.PowerShell.EditorServices.Engine.Server;
using Serilog;

namespace Microsoft.PowerShell.EditorServices.Engine
Expand Down Expand Up @@ -62,8 +57,6 @@ public class EditorServicesHost
{
#region Private Fields

private readonly IServiceCollection _serviceCollection;

private readonly HostDetails _hostDetails;

private readonly PSHost _internalHost;
Expand All @@ -74,7 +67,7 @@ public class EditorServicesHost

private readonly string[] _additionalModules;

private ILanguageServer _languageServer;
private PsesLanguageServer _languageServer;

private Microsoft.Extensions.Logging.ILogger _logger;

Expand Down Expand Up @@ -139,16 +132,15 @@ public EditorServicesHost(
Validate.IsNotNull(nameof(hostDetails), hostDetails);
Validate.IsNotNull(nameof(internalHost), internalHost);

_serviceCollection = new ServiceCollection();
_hostDetails = hostDetails;

//this._hostDetails = hostDetails;
this._enableConsoleRepl = enableConsoleRepl;
_enableConsoleRepl = enableConsoleRepl;
//this.bundledModulesPath = bundledModulesPath;
this._additionalModules = additionalModules ?? Array.Empty<string>();
this._featureFlags = new HashSet<string>(featureFlags ?? Array.Empty<string>());
_additionalModules = additionalModules ?? Array.Empty<string>();
_featureFlags = new HashSet<string>(featureFlags ?? Array.Empty<string>());
//this.serverCompletedTask = new TaskCompletionSource<bool>();
this._internalHost = internalHost;
_internalHost = internalHost;

#if DEBUG
if (waitForDebugger)
Expand Down Expand Up @@ -236,59 +228,33 @@ public void StartLanguageService(

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

_serviceCollection
.AddSingleton<WorkspaceService>()
.AddSingleton<SymbolsService>()
.AddSingleton<ConfigurationService>()
.AddSingleton<PowerShellContextService>(
(provider) =>
GetFullyInitializedPowerShellContext(
provider.GetService<OmniSharp.Extensions.LanguageServer.Protocol.Server.ILanguageServer>(),
profilePaths))
.AddSingleton<TemplateService>()
.AddSingleton<EditorOperationsService>()
.AddSingleton<ExtensionService>(
(provider) =>
{
var extensionService = new ExtensionService(
provider.GetService<PowerShellContextService>(),
provider.GetService<OmniSharp.Extensions.LanguageServer.Protocol.Server.ILanguageServer>());
extensionService.InitializeAsync(
serviceProvider: provider,
editorOperations: provider.GetService<EditorOperationsService>())
.Wait();
return extensionService;
})
.AddSingleton<AnalysisService>(
(provider) =>
{
return AnalysisService.Create(
provider.GetService<ConfigurationService>(),
provider.GetService<OmniSharp.Extensions.LanguageServer.Protocol.Server.ILanguageServer>(),
_factory.CreateLogger<AnalysisService>());
});


switch (config.TransportType)
{
case EditorServiceTransportType.NamedPipe:
_languageServer = new OmnisharpLanguageServerBuilder(_serviceCollection)
{
NamedPipeName = config.InOutPipeName ?? config.InPipeName,
OutNamedPipeName = config.OutPipeName,
LoggerFactory = _factory,
MinimumLogLevel = LogLevel.Trace,
}
.BuildLanguageServer();
_languageServer = new NamedPipePsesLanguageServer(
_factory,
LogLevel.Trace,
_enableConsoleRepl,
_featureFlags,
_hostDetails,
_additionalModules,
_internalHost,
profilePaths,
config.InOutPipeName ?? config.InPipeName,
config.OutPipeName);
break;

case EditorServiceTransportType.Stdio:
_languageServer = new OmnisharpLanguageServerBuilder(_serviceCollection)
{
Stdio = true,
LoggerFactory = _factory,
MinimumLogLevel = LogLevel.Trace,
}
.BuildLanguageServer();
_languageServer = new StdioPsesLanguageServer(
_factory,
LogLevel.Trace,
_featureFlags,
_hostDetails,
_additionalModules,
_internalHost,
profilePaths);
break;
}

Expand All @@ -302,57 +268,6 @@ public void StartLanguageService(
config.TransportType, config.Endpoint));
}

private PowerShellContextService GetFullyInitializedPowerShellContext(
OmniSharp.Extensions.LanguageServer.Protocol.Server.ILanguageServer languageServer,
ProfilePaths profilePaths)
{
var logger = _factory.CreateLogger<PowerShellContextService>();

// PSReadLine can only be used when -EnableConsoleRepl is specified otherwise
// issues arise when redirecting stdio.
var powerShellContext = new PowerShellContextService(
logger,
languageServer,
_featureFlags.Contains("PSReadLine") && _enableConsoleRepl);

EditorServicesPSHostUserInterface hostUserInterface =
_enableConsoleRepl
? (EditorServicesPSHostUserInterface) new TerminalPSHostUserInterface(powerShellContext, logger, _internalHost)
: new ProtocolPSHostUserInterface(languageServer, powerShellContext, logger);

EditorServicesPSHost psHost =
new EditorServicesPSHost(
powerShellContext,
_hostDetails,
hostUserInterface,
logger);

Runspace initialRunspace = PowerShellContextService.CreateRunspace(psHost);
powerShellContext.Initialize(profilePaths, initialRunspace, true, hostUserInterface);

powerShellContext.ImportCommandsModuleAsync(
Path.Combine(
Path.GetDirectoryName(this.GetType().GetTypeInfo().Assembly.Location),
@"..\Commands"));

// TODO: This can be moved to the point after the $psEditor object
// gets initialized when that is done earlier than LanguageServer.Initialize
foreach (string module in this._additionalModules)
{
var command =
new PSCommand()
.AddCommand("Microsoft.PowerShell.Core\\Import-Module")
.AddParameter("Name", module);

powerShellContext.ExecuteCommandAsync<PSObject>(
command,
sendOutputToHost: false,
sendErrorToHost: true);
}

return powerShellContext;
}

/// <summary>
/// Starts the debug service with the specified config.
/// </summary>
Expand Down
11 changes: 0 additions & 11 deletions src/PowerShellEditorServices.Engine/Interface/ILanguageServer.cs

This file was deleted.

This file was deleted.

Loading