17
17
using System . Threading . Tasks ;
18
18
using Microsoft . Extensions . DependencyInjection ;
19
19
using Microsoft . Extensions . Logging ;
20
+ using Microsoft . PowerShell . EditorServices . Extensions ;
20
21
using Serilog ;
21
22
22
23
namespace Microsoft . PowerShell . EditorServices . Engine
@@ -233,6 +234,56 @@ public void StartLanguageService(
233
234
234
235
_logger . LogInformation ( $ "LSP NamedPipe: { config . InOutPipeName } \n LSP OutPipe: { config . OutPipeName } ") ;
235
236
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
+ {
236
287
var logger = _factory . CreateLogger < PowerShellContextService > ( ) ;
237
288
var powerShellContext = new PowerShellContextService (
238
289
logger ,
@@ -244,7 +295,7 @@ public void StartLanguageService(
244
295
// ? (EditorServicesPSHostUserInterface)new TerminalPSHostUserInterface(powerShellContext, logger, _internalHost)
245
296
// : new ProtocolPSHostUserInterface(powerShellContext, messageSender, logger);
246
297
EditorServicesPSHostUserInterface hostUserInterface =
247
- ( EditorServicesPSHostUserInterface ) new TerminalPSHostUserInterface ( powerShellContext , logger , _internalHost ) ;
298
+ new TerminalPSHostUserInterface ( powerShellContext , logger , _internalHost ) ;
248
299
249
300
250
301
EditorServicesPSHost psHost =
@@ -267,51 +318,17 @@ public void StartLanguageService(
267
318
foreach ( string module in this . _additionalModules )
268
319
{
269
320
var command =
270
- new System . Management . Automation . PSCommand ( )
321
+ new PSCommand ( )
271
322
. AddCommand ( "Microsoft.PowerShell.Core\\ Import-Module" )
272
323
. AddParameter ( "Name" , module ) ;
273
324
274
- powerShellContext . ExecuteCommandAsync < System . Management . Automation . PSObject > (
325
+ powerShellContext . ExecuteCommandAsync < PSObject > (
275
326
command ,
276
327
sendOutputToHost : false ,
277
328
sendErrorToHost : true ) ;
278
329
}
279
330
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 ;
315
332
}
316
333
317
334
/// <summary>
0 commit comments