From 29b81d2d7fd31c5f0576754454005061e4b357b3 Mon Sep 17 00:00:00 2001 From: Tyler Leonhardt Date: Tue, 24 Mar 2020 13:07:35 -0700 Subject: [PATCH 1/4] Only run diagnostics on PowerShell files --- .../Services/Analysis/AnalysisService.cs | 2 +- .../Services/TextDocument/Handlers/TextDocumentHandler.cs | 7 +++++-- 2 files changed, 6 insertions(+), 3 deletions(-) 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/TextDocumentHandler.cs b/src/PowerShellEditorServices/Services/TextDocument/Handlers/TextDocumentHandler.cs index 784593d83..2a709db99 100644 --- a/src/PowerShellEditorServices/Services/TextDocument/Handlers/TextDocumentHandler.cs +++ b/src/PowerShellEditorServices/Services/TextDocument/Handlers/TextDocumentHandler.cs @@ -82,9 +82,12 @@ public Task Handle(DidOpenTextDocumentParams notification, CancellationTok notification.TextDocument.Uri, notification.TextDocument.Text); - // Kick off script diagnostics without blocking the response + // 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); + if (notification.TextDocument.LanguageId == "powershell") + { + _analysisService.RunScriptDiagnostics(new ScriptFile[] { openedFile }, token); + } _logger.LogTrace("Finished opening document."); return Unit.Task; From 4dc78198a414471fcd521995b584d18216c4b5ea Mon Sep 17 00:00:00 2001 From: Tyler Leonhardt Date: Tue, 24 Mar 2020 13:20:25 -0700 Subject: [PATCH 2/4] add a test and use PowerShellDocumentSelector --- .../TextDocument/Handlers/ReferencesHandler.cs | 4 ++-- .../TextDocument/Handlers/TextDocumentHandler.cs | 10 +++++++--- .../LanguageServerProtocolMessageTests.cs | 13 +++++++++++-- 3 files changed, 20 insertions(+), 7 deletions(-) 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 2a709db99..6c00a1ea1 100644 --- a/src/PowerShellEditorServices/Services/TextDocument/Handlers/TextDocumentHandler.cs +++ b/src/PowerShellEditorServices/Services/TextDocument/Handlers/TextDocumentHandler.cs @@ -82,10 +82,14 @@ public Task Handle(DidOpenTextDocumentParams notification, CancellationTok notification.TextDocument.Uri, notification.TextDocument.Text); - // Kick off script diagnostics if we got a PowerShell file without blocking the response - // TODO: Get all recently edited files in the workspace - if (notification.TextDocument.LanguageId == "powershell") + if (LspUtils.PowerShellDocumentSelector.IsMatch(new TextDocumentAttributes( + // We use null because we only want to test the LanguageId here and not if the + // file ends in ps*1. + null, + 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); } 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() { From 97eb79293ce6038d54c96a23e9af8bbe58967857 Mon Sep 17 00:00:00 2001 From: Tyler Leonhardt Date: Tue, 24 Mar 2020 13:36:59 -0700 Subject: [PATCH 3/4] use fake uri instead of null --- .../Services/TextDocument/Handlers/TextDocumentHandler.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/PowerShellEditorServices/Services/TextDocument/Handlers/TextDocumentHandler.cs b/src/PowerShellEditorServices/Services/TextDocument/Handlers/TextDocumentHandler.cs index 6c00a1ea1..bd4bbe005 100644 --- a/src/PowerShellEditorServices/Services/TextDocument/Handlers/TextDocumentHandler.cs +++ b/src/PowerShellEditorServices/Services/TextDocument/Handlers/TextDocumentHandler.cs @@ -83,9 +83,9 @@ public Task Handle(DidOpenTextDocumentParams notification, CancellationTok notification.TextDocument.Text); if (LspUtils.PowerShellDocumentSelector.IsMatch(new TextDocumentAttributes( - // We use null because we only want to test the LanguageId here and not if the + // We use a fake Uri because we only want to test the LanguageId here and not if the // file ends in ps*1. - null, + new Uri("Untitled:file"), notification.TextDocument.LanguageId))) { // Kick off script diagnostics if we got a PowerShell file without blocking the response From 04320a1ab4fd3816ad8f4e66fc85cd4d57deeb0c Mon Sep 17 00:00:00 2001 From: Tyler Leonhardt Date: Tue, 24 Mar 2020 14:49:45 -0700 Subject: [PATCH 4/4] cache uri --- .../Services/TextDocument/Handlers/TextDocumentHandler.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/PowerShellEditorServices/Services/TextDocument/Handlers/TextDocumentHandler.cs b/src/PowerShellEditorServices/Services/TextDocument/Handlers/TextDocumentHandler.cs index bd4bbe005..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; @@ -85,7 +86,7 @@ public Task Handle(DidOpenTextDocumentParams notification, CancellationTok 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. - new Uri("Untitled:file"), + s_fakeUri, notification.TextDocument.LanguageId))) { // Kick off script diagnostics if we got a PowerShell file without blocking the response