4
4
//
5
5
6
6
using System ;
7
+ using System . Collections . Generic ;
7
8
using System . Diagnostics ;
9
+ using System . IO ;
8
10
using System . Linq ;
9
11
using System . Management . Automation ;
10
12
using System . Management . Automation . Host ;
13
+ using System . Management . Automation . Runspaces ;
11
14
using System . Reflection ;
12
15
using System . Runtime . InteropServices ;
13
16
using System . Threading ;
@@ -60,11 +63,19 @@ public class EditorServicesHost
60
63
61
64
private readonly HostDetails _hostDetails ;
62
65
66
+ private readonly PSHost _internalHost ;
67
+
68
+ private readonly bool _enableConsoleRepl ;
69
+
70
+ private readonly HashSet < string > _featureFlags ;
71
+
72
+ private readonly string [ ] _additionalModules ;
73
+
63
74
private ILanguageServer _languageServer ;
64
75
65
- private readonly Extensions . Logging . ILogger _logger ;
76
+ private Microsoft . Extensions . Logging . ILogger _logger ;
66
77
67
- private readonly ILoggerFactory _factory ;
78
+ private ILoggerFactory _factory ;
68
79
69
80
#endregion
70
81
@@ -126,24 +137,15 @@ public EditorServicesHost(
126
137
Validate . IsNotNull ( nameof ( internalHost ) , internalHost ) ;
127
138
128
139
_serviceCollection = new ServiceCollection ( ) ;
129
-
130
- Log . Logger = new LoggerConfiguration ( ) . Enrich . FromLogContext ( )
131
- . WriteTo . Console ( )
132
- . CreateLogger ( ) ;
133
- _factory = new LoggerFactory ( ) . AddSerilog ( Log . Logger ) ;
134
- _logger = _factory . CreateLogger < EditorServicesHost > ( ) ;
135
-
136
140
_hostDetails = hostDetails ;
137
141
138
- /*
139
- this.hostDetails = hostDetails;
140
- this.enableConsoleRepl = enableConsoleRepl;
141
- this.bundledModulesPath = bundledModulesPath;
142
- this.additionalModules = additionalModules ?? Array.Empty<string>();
143
- this.featureFlags = new HashSet<string>(featureFlags ?? Array.Empty<string>();
144
- this.serverCompletedTask = new TaskCompletionSource<bool>();
145
- this.internalHost = internalHost;
146
- */
142
+ //this._hostDetails = hostDetails;
143
+ this . _enableConsoleRepl = enableConsoleRepl ;
144
+ //this.bundledModulesPath = bundledModulesPath;
145
+ this . _additionalModules = additionalModules ?? Array . Empty < string > ( ) ;
146
+ this . _featureFlags = new HashSet < string > ( featureFlags ?? Array . Empty < string > ( ) ) ;
147
+ //this.serverCompletedTask = new TaskCompletionSource<bool>();
148
+ this . _internalHost = internalHost ;
147
149
148
150
#if DEBUG
149
151
if ( waitForDebugger )
@@ -174,6 +176,12 @@ public EditorServicesHost(
174
176
/// <param name="logLevel">The minimum level of log messages to be written.</param>
175
177
public void StartLogging ( string logFilePath , PsesLogLevel logLevel )
176
178
{
179
+ Log . Logger = new LoggerConfiguration ( ) . Enrich . FromLogContext ( )
180
+ . WriteTo . File ( logFilePath )
181
+ . CreateLogger ( ) ;
182
+ _factory = new LoggerFactory ( ) . AddSerilog ( Log . Logger ) ;
183
+ _logger = _factory . CreateLogger < EditorServicesHost > ( ) ;
184
+
177
185
FileVersionInfo fileVersionInfo =
178
186
FileVersionInfo . GetVersionInfo ( this . GetType ( ) . GetTypeInfo ( ) . Assembly . Location ) ;
179
187
@@ -184,7 +192,7 @@ public void StartLogging(string logFilePath, PsesLogLevel logLevel)
184
192
string buildTime = BuildInfo . BuildTime ? . ToString ( "s" , System . Globalization . CultureInfo . InvariantCulture ) ?? "<unspecified>" ;
185
193
186
194
string logHeader = $@ "
187
- PowerShell Editor Services Host v{ fileVersionInfo . FileVersion } starting (PID { Process . GetCurrentProcess ( ) . Id }
195
+ PowerShell Editor Services Host v{ fileVersionInfo . FileVersion } starting (PID { Process . GetCurrentProcess ( ) . Id } )
188
196
189
197
Host application details:
190
198
@@ -219,23 +227,69 @@ public void StartLanguageService(
219
227
{
220
228
while ( System . Diagnostics . Debugger . IsAttached )
221
229
{
222
- Console . WriteLine ( $ "{ Process . GetCurrentProcess ( ) . Id } ") ;
230
+ System . Console . WriteLine ( $ "{ Process . GetCurrentProcess ( ) . Id } ") ;
223
231
Thread . Sleep ( 2000 ) ;
224
232
}
225
233
226
234
_logger . LogInformation ( $ "LSP NamedPipe: { config . InOutPipeName } \n LSP OutPipe: { config . OutPipeName } ") ;
227
235
228
- _serviceCollection . AddSingleton < WorkspaceService > ( ) ;
229
- _serviceCollection . AddSingleton < SymbolsService > ( ) ;
230
- _serviceCollection . AddSingleton < ConfigurationService > ( ) ;
231
- _serviceCollection . AddSingleton < AnalysisService > (
232
- ( provider ) => {
233
- return AnalysisService . Create (
234
- provider . GetService < ConfigurationService > ( ) ,
235
- provider . GetService < OmniSharp . Extensions . LanguageServer . Protocol . Server . ILanguageServer > ( ) ,
236
- _factory . CreateLogger < AnalysisService > ( ) ) ;
237
- }
238
- ) ;
236
+ var logger = _factory . CreateLogger < PowerShellContextService > ( ) ;
237
+ var powerShellContext = new PowerShellContextService (
238
+ logger ,
239
+ _featureFlags . Contains ( "PSReadLine" ) ) ;
240
+
241
+ // TODO: Bring this back
242
+ //EditorServicesPSHostUserInterface hostUserInterface =
243
+ // _enableConsoleRepl
244
+ // ? (EditorServicesPSHostUserInterface)new TerminalPSHostUserInterface(powerShellContext, logger, _internalHost)
245
+ // : new ProtocolPSHostUserInterface(powerShellContext, messageSender, logger);
246
+ EditorServicesPSHostUserInterface hostUserInterface =
247
+ ( EditorServicesPSHostUserInterface ) new TerminalPSHostUserInterface ( powerShellContext , logger , _internalHost ) ;
248
+
249
+
250
+ EditorServicesPSHost psHost =
251
+ new EditorServicesPSHost (
252
+ powerShellContext ,
253
+ _hostDetails ,
254
+ hostUserInterface ,
255
+ logger ) ;
256
+
257
+ Runspace initialRunspace = PowerShellContextService . CreateRunspace ( psHost ) ;
258
+ powerShellContext . Initialize ( profilePaths , initialRunspace , true , hostUserInterface ) ;
259
+
260
+ powerShellContext . ImportCommandsModuleAsync (
261
+ Path . Combine (
262
+ Path . GetDirectoryName ( this . GetType ( ) . GetTypeInfo ( ) . Assembly . Location ) ,
263
+ @"..\Commands" ) ) ;
264
+
265
+ // TODO: This can be moved to the point after the $psEditor object
266
+ // gets initialized when that is done earlier than LanguageServer.Initialize
267
+ foreach ( string module in this . _additionalModules )
268
+ {
269
+ var command =
270
+ new System . Management . Automation . PSCommand ( )
271
+ . AddCommand ( "Microsoft.PowerShell.Core\\ Import-Module" )
272
+ . AddParameter ( "Name" , module ) ;
273
+
274
+ powerShellContext . ExecuteCommandAsync < System . Management . Automation . PSObject > (
275
+ command ,
276
+ sendOutputToHost : false ,
277
+ sendErrorToHost : true ) ;
278
+ }
279
+
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
+ ) ;
239
293
240
294
_languageServer = new OmnisharpLanguageServerBuilder ( _serviceCollection )
241
295
{
@@ -248,10 +302,11 @@ public void StartLanguageService(
248
302
249
303
_logger . LogInformation ( "Starting language server" ) ;
250
304
251
- Task . Factory . StartNew ( ( ) => _languageServer . StartAsync ( ) ,
252
- CancellationToken . None ,
253
- TaskCreationOptions . LongRunning ,
254
- TaskScheduler . Default ) ;
305
+ Task . Run ( _languageServer . StartAsync ) ;
306
+ //Task.Factory.StartNew(() => _languageServer.StartAsync(),
307
+ // CancellationToken.None,
308
+ // TaskCreationOptions.LongRunning,
309
+ // TaskScheduler.Default);
255
310
256
311
_logger . LogInformation (
257
312
string . Format (
0 commit comments