Skip to content

Commit f25004c

Browse files
support $psEditor (#1006)
* support $psEditor * deleted commented out code * fix initial build failures due to lack of certain assemblies * use different RootPath * wait an extra 5 seconds just in case * refactor initialize script
1 parent dc8dfbf commit f25004c

21 files changed

+508
-360
lines changed

Diff for: PowerShellEditorServices.build.ps1

+12-31
Original file line numberDiff line numberDiff line change
@@ -51,46 +51,27 @@ Schema is:
5151
#>
5252
$script:RequiredBuildAssets = @{
5353
$script:ModuleBinPath = @{
54-
'PowerShellEditorServices' = @(
55-
'publish/Serilog.dll',
56-
'publish/Serilog.Sinks.Async.dll',
57-
'publish/Serilog.Sinks.Console.dll',
58-
'publish/Serilog.Sinks.File.dll',
59-
'publish/Microsoft.Extensions.FileSystemGlobbing.dll',
60-
'Microsoft.PowerShell.EditorServices.dll',
61-
'Microsoft.PowerShell.EditorServices.pdb'
62-
)
63-
64-
'PowerShellEditorServices.Host' = @(
65-
'publish/UnixConsoleEcho.dll',
66-
'publish/runtimes/osx-64/native/libdisablekeyecho.dylib',
67-
'publish/runtimes/linux-64/native/libdisablekeyecho.so',
68-
'publish/Newtonsoft.Json.dll',
69-
'Microsoft.PowerShell.EditorServices.Host.dll',
70-
'Microsoft.PowerShell.EditorServices.Host.pdb'
71-
)
72-
73-
'PowerShellEditorServices.Protocol' = @(
74-
'Microsoft.PowerShell.EditorServices.Protocol.dll',
75-
'Microsoft.PowerShell.EditorServices.Protocol.pdb'
76-
)
77-
7854
'PowerShellEditorServices.Engine' = @(
55+
'publish/Microsoft.Extensions.DependencyInjection.Abstractions.dll',
56+
'publish/Microsoft.Extensions.DependencyInjection.dll',
57+
'publish/Microsoft.Extensions.FileSystemGlobbing.dll',
58+
'publish/Microsoft.Extensions.Logging.Abstractions.dll',
59+
'publish/Microsoft.Extensions.Logging.dll',
60+
'publish/Microsoft.Extensions.Options.dll',
61+
'publish/Microsoft.Extensions.Primitives.dll',
7962
'publish/Microsoft.PowerShell.EditorServices.Engine.dll',
8063
'publish/Microsoft.PowerShell.EditorServices.Engine.pdb',
64+
'publish/Newtonsoft.Json.dll',
8165
'publish/OmniSharp.Extensions.JsonRpc.dll',
8266
'publish/OmniSharp.Extensions.LanguageProtocol.dll',
8367
'publish/OmniSharp.Extensions.LanguageServer.dll',
68+
'publish/runtimes/linux-64/native/libdisablekeyecho.so',
69+
'publish/runtimes/osx-64/native/libdisablekeyecho.dylib',
8470
'publish/Serilog.dll',
8571
'publish/Serilog.Extensions.Logging.dll',
8672
'publish/Serilog.Sinks.File.dll',
87-
'publish/Microsoft.Extensions.DependencyInjection.Abstractions.dll',
88-
'publish/Microsoft.Extensions.DependencyInjection.dll',
89-
'publish/Microsoft.Extensions.Logging.Abstractions.dll',
90-
'publish/Microsoft.Extensions.Logging.dll',
91-
'publish/Microsoft.Extensions.Options.dll',
92-
'publish/Microsoft.Extensions.Primitives.dll',
93-
'publish/System.Reactive.dll'
73+
'publish/System.Reactive.dll',
74+
'publish/UnixConsoleEcho.dll'
9475
)
9576
}
9677

Diff for: module/PowerShellEditorServices/PowerShellEditorServices.psm1

-3
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,6 @@ if ($PSEdition -eq 'Desktop') {
1010
Microsoft.PowerShell.Utility\Add-Type -Path "$PSScriptRoot/bin/Desktop/System.Security.Principal.Windows.dll"
1111
}
1212

13-
Microsoft.PowerShell.Utility\Add-Type -Path "$PSScriptRoot/bin/Microsoft.PowerShell.EditorServices.dll"
14-
Microsoft.PowerShell.Utility\Add-Type -Path "$PSScriptRoot/bin/Microsoft.PowerShell.EditorServices.Host.dll"
15-
Microsoft.PowerShell.Utility\Add-Type -Path "$PSScriptRoot/bin/Microsoft.PowerShell.EditorServices.Protocol.dll"
1613
Microsoft.PowerShell.Utility\Add-Type -Path "$PSScriptRoot/bin/Microsoft.PowerShell.EditorServices.Engine.dll"
1714

1815
function Start-EditorServicesHost {

Diff for: src/PowerShellEditorServices.Engine/Hosting/EditorServicesHost.cs

+55-38
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
using System.Threading.Tasks;
1818
using Microsoft.Extensions.DependencyInjection;
1919
using Microsoft.Extensions.Logging;
20+
using Microsoft.PowerShell.EditorServices.Extensions;
2021
using Serilog;
2122

2223
namespace Microsoft.PowerShell.EditorServices.Engine
@@ -233,6 +234,56 @@ public void StartLanguageService(
233234

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

237+
var powerShellContext = GetFullyInitializedPowerShellContext(profilePaths);
238+
239+
_serviceCollection
240+
.AddSingleton<WorkspaceService>()
241+
.AddSingleton<SymbolsService>()
242+
.AddSingleton<ConfigurationService>()
243+
.AddSingleton<PowerShellContextService>(powerShellContext)
244+
.AddSingleton<EditorOperationsService>()
245+
.AddSingleton<ExtensionService>(
246+
(provider) =>
247+
{
248+
var extensionService = new ExtensionService(
249+
provider.GetService<PowerShellContextService>(),
250+
provider.GetService<OmniSharp.Extensions.LanguageServer.Protocol.Server.ILanguageServer>());
251+
extensionService.InitializeAsync(
252+
serviceProvider: provider,
253+
editorOperations: provider.GetService<EditorOperationsService>())
254+
.Wait();
255+
return extensionService;
256+
})
257+
.AddSingleton<AnalysisService>(
258+
(provider) =>
259+
{
260+
return AnalysisService.Create(
261+
provider.GetService<ConfigurationService>(),
262+
provider.GetService<OmniSharp.Extensions.LanguageServer.Protocol.Server.ILanguageServer>(),
263+
_factory.CreateLogger<AnalysisService>());
264+
});
265+
266+
_languageServer = new OmnisharpLanguageServerBuilder(_serviceCollection)
267+
{
268+
NamedPipeName = config.InOutPipeName ?? config.InPipeName,
269+
OutNamedPipeName = config.OutPipeName,
270+
LoggerFactory = _factory,
271+
MinimumLogLevel = LogLevel.Trace,
272+
}
273+
.BuildLanguageServer();
274+
275+
_logger.LogInformation("Starting language server");
276+
277+
Task.Run(_languageServer.StartAsync);
278+
279+
_logger.LogInformation(
280+
string.Format(
281+
"Language service started, type = {0}, endpoint = {1}",
282+
config.TransportType, config.Endpoint));
283+
}
284+
285+
private PowerShellContextService GetFullyInitializedPowerShellContext(ProfilePaths profilePaths)
286+
{
236287
var logger = _factory.CreateLogger<PowerShellContextService>();
237288
var powerShellContext = new PowerShellContextService(
238289
logger,
@@ -244,7 +295,7 @@ public void StartLanguageService(
244295
// ? (EditorServicesPSHostUserInterface)new TerminalPSHostUserInterface(powerShellContext, logger, _internalHost)
245296
// : new ProtocolPSHostUserInterface(powerShellContext, messageSender, logger);
246297
EditorServicesPSHostUserInterface hostUserInterface =
247-
(EditorServicesPSHostUserInterface)new TerminalPSHostUserInterface(powerShellContext, logger, _internalHost);
298+
new TerminalPSHostUserInterface(powerShellContext, logger, _internalHost);
248299

249300

250301
EditorServicesPSHost psHost =
@@ -267,51 +318,17 @@ public void StartLanguageService(
267318
foreach (string module in this._additionalModules)
268319
{
269320
var command =
270-
new System.Management.Automation.PSCommand()
321+
new PSCommand()
271322
.AddCommand("Microsoft.PowerShell.Core\\Import-Module")
272323
.AddParameter("Name", module);
273324

274-
powerShellContext.ExecuteCommandAsync<System.Management.Automation.PSObject>(
325+
powerShellContext.ExecuteCommandAsync<PSObject>(
275326
command,
276327
sendOutputToHost: false,
277328
sendErrorToHost: true);
278329
}
279330

280-
_serviceCollection
281-
.AddSingleton<WorkspaceService>()
282-
.AddSingleton<SymbolsService>()
283-
.AddSingleton<ConfigurationService>()
284-
.AddSingleton<PowerShellContextService>(powerShellContext)
285-
.AddSingleton<AnalysisService>(
286-
(provider) => {
287-
return AnalysisService.Create(
288-
provider.GetService<ConfigurationService>(),
289-
provider.GetService<OmniSharp.Extensions.LanguageServer.Protocol.Server.ILanguageServer>(),
290-
_factory.CreateLogger<AnalysisService>());
291-
}
292-
);
293-
294-
_languageServer = new OmnisharpLanguageServerBuilder(_serviceCollection)
295-
{
296-
NamedPipeName = config.InOutPipeName ?? config.InPipeName,
297-
OutNamedPipeName = config.OutPipeName,
298-
LoggerFactory = _factory,
299-
MinimumLogLevel = LogLevel.Trace,
300-
}
301-
.BuildLanguageServer();
302-
303-
_logger.LogInformation("Starting language server");
304-
305-
Task.Run(_languageServer.StartAsync);
306-
//Task.Factory.StartNew(() => _languageServer.StartAsync(),
307-
// CancellationToken.None,
308-
// TaskCreationOptions.LongRunning,
309-
// TaskScheduler.Default);
310-
311-
_logger.LogInformation(
312-
string.Format(
313-
"Language service started, type = {0}, endpoint = {1}",
314-
config.TransportType, config.Endpoint));
331+
return powerShellContext;
315332
}
316333

317334
/// <summary>

Diff for: src/PowerShellEditorServices.Engine/LanguageServer/OmnisharpLanguageServer.cs

+21-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
using OmniSharp.Extensions.LanguageServer.Server;
1515
using PowerShellEditorServices.Engine.Services.Handlers;
1616
using Microsoft.PowerShell.EditorServices.TextDocument;
17+
using System.IO;
1718

1819
namespace Microsoft.PowerShell.EditorServices.Engine
1920
{
@@ -95,7 +96,26 @@ public async Task StartAsync()
9596
.WithHandler<DocumentHighlightHandler>()
9697
.WithHandler<PSHostProcessAndRunspaceHandlers>()
9798
.WithHandler<CodeLensHandlers>()
98-
.WithHandler<CodeActionHandler>();
99+
.WithHandler<CodeActionHandler>()
100+
.WithHandler<InvokeExtensionCommandHandler>()
101+
.OnInitialize(
102+
async (languageServer, request) =>
103+
{
104+
var serviceProvider = languageServer.Services;
105+
var workspaceService = serviceProvider.GetService<WorkspaceService>();
106+
107+
// Grab the workspace path from the parameters
108+
workspaceService.WorkspacePath = request.RootPath;
109+
110+
// Set the working directory of the PowerShell session to the workspace path
111+
if (workspaceService.WorkspacePath != null
112+
&& Directory.Exists(workspaceService.WorkspacePath))
113+
{
114+
await serviceProvider.GetService<PowerShellContextService>().SetWorkingDirectoryAsync(
115+
workspaceService.WorkspacePath,
116+
isPathAlreadyEscaped: false);
117+
}
118+
});
99119

100120
logger.LogInformation("Handlers added");
101121
});

Diff for: src/PowerShellEditorServices.Engine/PowerShellEditorServices.Engine.csproj

-7
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,7 @@
2828
<PackageReference Include="System.IO.Pipes.AccessControl" Version="4.5.1" />
2929
<PackageReference Include="System.Security.Principal" Version="4.3.0" />
3030
<PackageReference Include="System.Security.Principal.Windows" Version="4.5.1" />
31-
<PackageReference Include="System.Threading.Tasks.Dataflow" Version="4.9.0" />
3231
<PackageReference Include="UnixConsoleEcho" Version="0.1.0" />
3332
</ItemGroup>
3433

35-
<ItemGroup>
36-
<Folder Include="Services\PowerShellContext\Session\" />
37-
<Folder Include="Services\PowerShellContext\Console\" />
38-
<Folder Include="Services\PowerShellContext\Extensions\" />
39-
<Folder Include="Services\PowerShellContext\Components\" />
40-
</ItemGroup>
4134
</Project>

Diff for: src/PowerShellEditorServices.Engine/Services/PowerShellContext/Components/ComponentRegistry.cs

-84
This file was deleted.

Diff for: src/PowerShellEditorServices.Engine/Services/PowerShellContext/Components/IComponentRegistry.cs

-61
This file was deleted.

0 commit comments

Comments
 (0)