@@ -25,6 +25,7 @@ internal class LanguageServer : IMessageProcessor
25
25
private static CancellationTokenSource existingRequestCancellation ;
26
26
27
27
private MessageDispatcher < EditorSession > messageDispatcher ;
28
+ private LanguageServerSettings currentSettings = new LanguageServerSettings ( ) ;
28
29
29
30
public LanguageServer ( )
30
31
{
@@ -42,6 +43,7 @@ public void Initialize()
42
43
this . AddEventHandler ( DidOpenTextDocumentNotification . Type , this . HandleDidOpenTextDocumentNotification ) ;
43
44
this . AddEventHandler ( DidCloseTextDocumentNotification . Type , this . HandleDidCloseTextDocumentNotification ) ;
44
45
this . AddEventHandler ( DidChangeTextDocumentNotification . Type , this . HandleDidChangeTextDocumentNotification ) ;
46
+ this . AddEventHandler ( DidChangeConfigurationNotification < SettingsWrapper > . Type , this . HandleDidChangeConfigurationNotification ) ;
45
47
46
48
this . AddRequestHandler ( DefinitionRequest . Type , this . HandleDefinitionRequest ) ;
47
49
this . AddRequestHandler ( ReferencesRequest . Type , this . HandleReferencesRequest ) ;
@@ -94,6 +96,9 @@ protected async Task HandleInitializeRequest(
94
96
EditorSession editorSession ,
95
97
RequestContext < InitializeResult , InitializeError > requestContext )
96
98
{
99
+ // Grab the workspace path from the parameters
100
+ editorSession . Workspace . WorkspacePath = initializeParams . RootPath ;
101
+
97
102
await requestContext . SendResult (
98
103
new InitializeResult
99
104
{
@@ -130,20 +135,19 @@ protected Task HandleShutdownRequest(
130
135
}
131
136
132
137
protected async Task HandleShowOnlineHelpRequest (
133
- object helpParams ,
138
+ string helpParams ,
134
139
EditorSession editorSession ,
135
140
RequestContext < object , object > requestContext )
136
141
{
137
- var psCommand = new PSCommand ( ) ;
138
-
139
142
if ( helpParams == null ) { helpParams = "get-help" ; }
140
143
141
- var script = string . Format ( "get-help {0} -Online" , helpParams ) ;
142
-
143
- psCommand . AddScript ( script ) ;
144
+ var psCommand = new PSCommand ( ) ;
145
+ psCommand . AddCommand ( "Get-Help" ) ;
146
+ psCommand . AddArgument ( helpParams ) ;
147
+ psCommand . AddParameter ( "Online" ) ;
144
148
145
- var result = await editorSession . powerShellContext . ExecuteCommand < object > (
146
- psCommand ) ;
149
+ await editorSession . PowerShellContext . ExecuteCommand < object > (
150
+ psCommand ) ;
147
151
148
152
await requestContext . SendResult ( null ) ;
149
153
}
@@ -226,6 +230,36 @@ protected Task HandleDidChangeTextDocumentNotification(
226
230
return Task . FromResult ( true ) ;
227
231
}
228
232
233
+ protected async Task HandleDidChangeConfigurationNotification (
234
+ DidChangeConfigurationParams < SettingsWrapper > configChangeParams ,
235
+ EditorSession editorSession ,
236
+ EventContext eventContext )
237
+ {
238
+ bool oldScriptAnalysisEnabled =
239
+ this . currentSettings . ScriptAnalysis . Enable . HasValue ;
240
+
241
+ this . currentSettings . Update (
242
+ configChangeParams . Settings . Powershell ) ;
243
+
244
+ if ( oldScriptAnalysisEnabled != this . currentSettings . ScriptAnalysis . Enable )
245
+ {
246
+ // If the user just turned off script analysis, send a diagnostics
247
+ // event to clear the analysis markers that they already have
248
+ if ( ! this . currentSettings . ScriptAnalysis . Enable . Value )
249
+ {
250
+ ScriptFileMarker [ ] emptyAnalysisDiagnostics = new ScriptFileMarker [ 0 ] ;
251
+
252
+ foreach ( var scriptFile in editorSession . Workspace . GetOpenedFiles ( ) )
253
+ {
254
+ await PublishScriptDiagnostics (
255
+ scriptFile ,
256
+ emptyAnalysisDiagnostics ,
257
+ eventContext ) ;
258
+ }
259
+ }
260
+ }
261
+ }
262
+
229
263
protected async Task HandleDefinitionRequest (
230
264
TextDocumentPosition textDocumentPosition ,
231
265
EditorSession editorSession ,
@@ -387,21 +421,18 @@ protected async Task HandleCompletionResolveRequest(
387
421
if ( completionItem . Kind == CompletionItemKind . Function )
388
422
{
389
423
RunspaceHandle runspaceHandle =
390
- await editorSession . powerShellContext . GetRunspaceHandle ( ) ;
424
+ await editorSession . PowerShellContext . GetRunspaceHandle ( ) ;
391
425
392
426
// Get the documentation for the function
393
427
CommandInfo commandInfo =
394
428
CommandHelpers . GetCommandInfo (
395
429
completionItem . Label ,
396
430
runspaceHandle . Runspace ) ;
397
431
398
- if ( commandInfo != null )
399
- {
400
- completionItem . Documentation =
401
- CommandHelpers . GetCommandSynopsis (
402
- commandInfo ,
403
- runspaceHandle . Runspace ) ;
404
- }
432
+ completionItem . Documentation =
433
+ CommandHelpers . GetCommandSynopsis (
434
+ commandInfo ,
435
+ runspaceHandle . Runspace ) ;
405
436
406
437
runspaceHandle . Dispose ( ) ;
407
438
}
@@ -744,6 +775,12 @@ private Task RunScriptDiagnostics(
744
775
EditorSession editorSession ,
745
776
EventContext eventContext )
746
777
{
778
+ if ( ! this . currentSettings . ScriptAnalysis . Enable . Value )
779
+ {
780
+ // If the user has disabled script analysis, skip it entirely
781
+ return TaskConstants . Completed ;
782
+ }
783
+
747
784
// If there's an existing task, attempt to cancel it
748
785
try
749
786
{
@@ -827,24 +864,36 @@ private static async Task DelayThenInvokeDiagnostics(
827
864
828
865
var allMarkers = scriptFile . SyntaxMarkers . Concat ( semanticMarkers ) ;
829
866
830
- // Always send syntax and semantic errors. We want to
831
- // make sure no out-of-date markers are being displayed.
832
- await eventContext . SendEvent (
833
- PublishDiagnosticsNotification . Type ,
834
- new PublishDiagnosticsNotification
835
- {
836
- Uri = scriptFile . ClientFilePath ,
837
- Diagnostics =
838
- allMarkers
839
- . Select ( GetDiagnosticFromMarker )
840
- . ToArray ( )
841
- } ) ;
842
-
867
+ await PublishScriptDiagnostics (
868
+ scriptFile ,
869
+ semanticMarkers ,
870
+ eventContext ) ;
843
871
}
844
872
845
873
Logger . Write ( LogLevel . Verbose , "Analysis complete." ) ;
846
874
}
847
875
876
+ private async static Task PublishScriptDiagnostics (
877
+ ScriptFile scriptFile ,
878
+ ScriptFileMarker [ ] semanticMarkers ,
879
+ EventContext eventContext )
880
+ {
881
+ var allMarkers = scriptFile . SyntaxMarkers . Concat ( semanticMarkers ) ;
882
+
883
+ // Always send syntax and semantic errors. We want to
884
+ // make sure no out-of-date markers are being displayed.
885
+ await eventContext . SendEvent (
886
+ PublishDiagnosticsNotification . Type ,
887
+ new PublishDiagnosticsNotification
888
+ {
889
+ Uri = scriptFile . ClientFilePath ,
890
+ Diagnostics =
891
+ allMarkers
892
+ . Select ( GetDiagnosticFromMarker )
893
+ . ToArray ( )
894
+ } ) ;
895
+ }
896
+
848
897
private static Diagnostic GetDiagnosticFromMarker ( ScriptFileMarker scriptFileMarker )
849
898
{
850
899
return new Diagnostic
0 commit comments