@@ -25,6 +25,7 @@ internal class LanguageServer : IMessageProcessor
2525 private static CancellationTokenSource existingRequestCancellation ;
2626
2727 private MessageDispatcher < EditorSession > messageDispatcher ;
28+ private LanguageServerSettings currentSettings = new LanguageServerSettings ( ) ;
2829
2930 public LanguageServer ( )
3031 {
@@ -42,6 +43,7 @@ public void Initialize()
4243 this . AddEventHandler ( DidOpenTextDocumentNotification . Type , this . HandleDidOpenTextDocumentNotification ) ;
4344 this . AddEventHandler ( DidCloseTextDocumentNotification . Type , this . HandleDidCloseTextDocumentNotification ) ;
4445 this . AddEventHandler ( DidChangeTextDocumentNotification . Type , this . HandleDidChangeTextDocumentNotification ) ;
46+ this . AddEventHandler ( DidChangeConfigurationNotification < SettingsWrapper > . Type , this . HandleDidChangeConfigurationNotification ) ;
4547
4648 this . AddRequestHandler ( DefinitionRequest . Type , this . HandleDefinitionRequest ) ;
4749 this . AddRequestHandler ( ReferencesRequest . Type , this . HandleReferencesRequest ) ;
@@ -94,6 +96,9 @@ protected async Task HandleInitializeRequest(
9496 EditorSession editorSession ,
9597 RequestContext < InitializeResult , InitializeError > requestContext )
9698 {
99+ // Grab the workspace path from the parameters
100+ editorSession . Workspace . WorkspacePath = initializeParams . RootPath ;
101+
97102 await requestContext . SendResult (
98103 new InitializeResult
99104 {
@@ -130,20 +135,19 @@ protected Task HandleShutdownRequest(
130135 }
131136
132137 protected async Task HandleShowOnlineHelpRequest (
133- object helpParams ,
138+ string helpParams ,
134139 EditorSession editorSession ,
135140 RequestContext < object , object > requestContext )
136141 {
137- var psCommand = new PSCommand ( ) ;
138-
139142 if ( helpParams == null ) { helpParams = "get-help" ; }
140143
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" ) ;
144148
145- var result = await editorSession . powerShellContext . ExecuteCommand < object > (
146- psCommand ) ;
149+ await editorSession . PowerShellContext . ExecuteCommand < object > (
150+ psCommand ) ;
147151
148152 await requestContext . SendResult ( null ) ;
149153 }
@@ -226,6 +230,36 @@ protected Task HandleDidChangeTextDocumentNotification(
226230 return Task . FromResult ( true ) ;
227231 }
228232
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+
229263 protected async Task HandleDefinitionRequest (
230264 TextDocumentPosition textDocumentPosition ,
231265 EditorSession editorSession ,
@@ -387,21 +421,18 @@ protected async Task HandleCompletionResolveRequest(
387421 if ( completionItem . Kind == CompletionItemKind . Function )
388422 {
389423 RunspaceHandle runspaceHandle =
390- await editorSession . powerShellContext . GetRunspaceHandle ( ) ;
424+ await editorSession . PowerShellContext . GetRunspaceHandle ( ) ;
391425
392426 // Get the documentation for the function
393427 CommandInfo commandInfo =
394428 CommandHelpers . GetCommandInfo (
395429 completionItem . Label ,
396430 runspaceHandle . Runspace ) ;
397431
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 ) ;
405436
406437 runspaceHandle . Dispose ( ) ;
407438 }
@@ -744,6 +775,12 @@ private Task RunScriptDiagnostics(
744775 EditorSession editorSession ,
745776 EventContext eventContext )
746777 {
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+
747784 // If there's an existing task, attempt to cancel it
748785 try
749786 {
@@ -827,24 +864,36 @@ private static async Task DelayThenInvokeDiagnostics(
827864
828865 var allMarkers = scriptFile . SyntaxMarkers . Concat ( semanticMarkers ) ;
829866
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 ) ;
843871 }
844872
845873 Logger . Write ( LogLevel . Verbose , "Analysis complete." ) ;
846874 }
847875
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+
848897 private static Diagnostic GetDiagnosticFromMarker ( ScriptFileMarker scriptFileMarker )
849898 {
850899 return new Diagnostic
0 commit comments