diff --git a/src/PowerShellEditorServices/Services/Analysis/AnalysisService.cs b/src/PowerShellEditorServices/Services/Analysis/AnalysisService.cs index ef7979e55..6b2d25d27 100644 --- a/src/PowerShellEditorServices/Services/Analysis/AnalysisService.cs +++ b/src/PowerShellEditorServices/Services/Analysis/AnalysisService.cs @@ -410,7 +410,7 @@ private async Task DelayThenInvokeDiagnosticsAsync(ScriptFile[] filesToAnalyze, private void PublishScriptDiagnostics(ScriptFile scriptFile, IReadOnlyList markers) { - var diagnostics = new Diagnostic[scriptFile.DiagnosticMarkers.Count]; + var diagnostics = new Diagnostic[markers.Count]; CorrectionTableEntry fileCorrections = _mostRecentCorrectionsByFile.GetOrAdd( scriptFile, diff --git a/src/PowerShellEditorServices/Services/TextDocument/Handlers/ReferencesHandler.cs b/src/PowerShellEditorServices/Services/TextDocument/Handlers/ReferencesHandler.cs index 9917c557f..81dc17f8c 100644 --- a/src/PowerShellEditorServices/Services/TextDocument/Handlers/ReferencesHandler.cs +++ b/src/PowerShellEditorServices/Services/TextDocument/Handlers/ReferencesHandler.cs @@ -39,7 +39,7 @@ public TextDocumentRegistrationOptions GetRegistrationOptions() }; } - public async Task Handle(ReferenceParams request, CancellationToken cancellationToken) + public Task Handle(ReferenceParams request, CancellationToken cancellationToken) { ScriptFile scriptFile = _workspaceService.GetFile(request.TextDocument.Uri); @@ -69,7 +69,7 @@ public async Task Handle(ReferenceParams request, Cancellatio } } - return new LocationContainer(locations); + return Task.FromResult(new LocationContainer(locations)); } public void SetCapability(ReferencesCapability capability) diff --git a/src/PowerShellEditorServices/Services/TextDocument/Handlers/TextDocumentHandler.cs b/src/PowerShellEditorServices/Services/TextDocument/Handlers/TextDocumentHandler.cs index 784593d83..73c779cac 100644 --- a/src/PowerShellEditorServices/Services/TextDocument/Handlers/TextDocumentHandler.cs +++ b/src/PowerShellEditorServices/Services/TextDocument/Handlers/TextDocumentHandler.cs @@ -21,6 +21,7 @@ namespace Microsoft.PowerShell.EditorServices.Handlers { class TextDocumentHandler : ITextDocumentSyncHandler { + private static readonly Uri s_fakeUri = new Uri("Untitled:fake"); private readonly ILogger _logger; private readonly AnalysisService _analysisService; @@ -82,9 +83,16 @@ public Task Handle(DidOpenTextDocumentParams notification, CancellationTok notification.TextDocument.Uri, notification.TextDocument.Text); - // Kick off script diagnostics without blocking the response - // TODO: Get all recently edited files in the workspace - _analysisService.RunScriptDiagnostics(new ScriptFile[] { openedFile }, token); + if (LspUtils.PowerShellDocumentSelector.IsMatch(new TextDocumentAttributes( + // We use a fake Uri because we only want to test the LanguageId here and not if the + // file ends in ps*1. + s_fakeUri, + notification.TextDocument.LanguageId))) + { + // Kick off script diagnostics if we got a PowerShell file without blocking the response + // TODO: Get all recently edited files in the workspace + _analysisService.RunScriptDiagnostics(new ScriptFile[] { openedFile }, token); + } _logger.LogTrace("Finished opening document."); return Unit.Task; diff --git a/test/PowerShellEditorServices.Test.E2E/LanguageServerProtocolMessageTests.cs b/test/PowerShellEditorServices.Test.E2E/LanguageServerProtocolMessageTests.cs index 8e1501814..5582cd68b 100644 --- a/test/PowerShellEditorServices.Test.E2E/LanguageServerProtocolMessageTests.cs +++ b/test/PowerShellEditorServices.Test.E2E/LanguageServerProtocolMessageTests.cs @@ -59,7 +59,7 @@ public void Dispose() Diagnostics.Clear(); } - private string NewTestFile(string script, bool isPester = false) + private string NewTestFile(string script, bool isPester = false, string languageId = "powershell") { string fileExt = isPester ? ".Tests.ps1" : ".ps1"; string filePath = Path.Combine(s_binDir, Path.GetRandomFileName() + fileExt); @@ -69,7 +69,7 @@ private string NewTestFile(string script, bool isPester = false) { TextDocument = new TextDocumentItem { - LanguageId = "powershell", + LanguageId = languageId, Version = 0, Text = script, Uri = new Uri(filePath) @@ -145,6 +145,15 @@ public async Task CanReceiveDiagnosticsFromFileOpen() Assert.Equal("PSUseDeclaredVarsMoreThanAssignments", diagnostic.Code); } + [Fact] + public async Task WontReceiveDiagnosticsFromFileOpenThatIsNotPowerShell() + { + NewTestFile("$a = 4", languageId: "plaintext"); + await Task.Delay(2000); + + Assert.Empty(Diagnostics); + } + [Fact] public async Task CanReceiveDiagnosticsFromFileChanged() {