@@ -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 ) ;
@@ -226,6 +228,36 @@ protected Task HandleDidChangeTextDocumentNotification(
226228 return Task . FromResult ( true ) ;
227229 }
228230
231+ protected async Task HandleDidChangeConfigurationNotification (
232+ DidChangeConfigurationParams < SettingsWrapper > configChangeParams ,
233+ EditorSession editorSession ,
234+ EventContext eventContext )
235+ {
236+ bool oldScriptAnalysisEnabled =
237+ this . currentSettings . ScriptAnalysis . Enable . HasValue ;
238+
239+ this . currentSettings . Update (
240+ configChangeParams . Settings . Powershell ) ;
241+
242+ if ( oldScriptAnalysisEnabled != this . currentSettings . ScriptAnalysis . Enable )
243+ {
244+ // If the user just turned off script analysis, send a diagnostics
245+ // event to clear the analysis markers that they already have
246+ if ( ! this . currentSettings . ScriptAnalysis . Enable . Value )
247+ {
248+ ScriptFileMarker [ ] emptyAnalysisDiagnostics = new ScriptFileMarker [ 0 ] ;
249+
250+ foreach ( var scriptFile in editorSession . Workspace . GetOpenedFiles ( ) )
251+ {
252+ await PublishScriptDiagnostics (
253+ scriptFile ,
254+ emptyAnalysisDiagnostics ,
255+ eventContext ) ;
256+ }
257+ }
258+ }
259+ }
260+
229261 protected async Task HandleDefinitionRequest (
230262 TextDocumentPosition textDocumentPosition ,
231263 EditorSession editorSession ,
@@ -744,6 +776,12 @@ private Task RunScriptDiagnostics(
744776 EditorSession editorSession ,
745777 EventContext eventContext )
746778 {
779+ if ( ! this . currentSettings . ScriptAnalysis . Enable . Value )
780+ {
781+ // If the user has disabled script analysis, skip it entirely
782+ return TaskConstants . Completed ;
783+ }
784+
747785 // If there's an existing task, attempt to cancel it
748786 try
749787 {
@@ -827,24 +865,36 @@ private static async Task DelayThenInvokeDiagnostics(
827865
828866 var allMarkers = scriptFile . SyntaxMarkers . Concat ( semanticMarkers ) ;
829867
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-
868+ await PublishScriptDiagnostics (
869+ scriptFile ,
870+ semanticMarkers ,
871+ eventContext ) ;
843872 }
844873
845874 Logger . Write ( LogLevel . Verbose , "Analysis complete." ) ;
846875 }
847876
877+ private async static Task PublishScriptDiagnostics (
878+ ScriptFile scriptFile ,
879+ ScriptFileMarker [ ] semanticMarkers ,
880+ EventContext eventContext )
881+ {
882+ var allMarkers = scriptFile . SyntaxMarkers . Concat ( semanticMarkers ) ;
883+
884+ // Always send syntax and semantic errors. We want to
885+ // make sure no out-of-date markers are being displayed.
886+ await eventContext . SendEvent (
887+ PublishDiagnosticsNotification . Type ,
888+ new PublishDiagnosticsNotification
889+ {
890+ Uri = scriptFile . ClientFilePath ,
891+ Diagnostics =
892+ allMarkers
893+ . Select ( GetDiagnosticFromMarker )
894+ . ToArray ( )
895+ } ) ;
896+ }
897+
848898 private static Diagnostic GetDiagnosticFromMarker ( ScriptFileMarker scriptFileMarker )
849899 {
850900 return new Diagnostic
0 commit comments