From 090780fc115a24e8108dd50c2e15d4be48539e80 Mon Sep 17 00:00:00 2001 From: Tyler Leonhardt Date: Thu, 31 Oct 2019 07:48:20 -0400 Subject: [PATCH 1/3] Protect against duplicate diagnostics --- .../Handlers/TextDocumentHandler.cs | 9 +++-- .../LanguageServerProtocolMessageTests.cs | 37 +++++++++++++++++++ 2 files changed, 43 insertions(+), 3 deletions(-) diff --git a/src/PowerShellEditorServices/Services/TextDocument/Handlers/TextDocumentHandler.cs b/src/PowerShellEditorServices/Services/TextDocument/Handlers/TextDocumentHandler.cs index 7239fbe6a..06075707b 100644 --- a/src/PowerShellEditorServices/Services/TextDocument/Handlers/TextDocumentHandler.cs +++ b/src/PowerShellEditorServices/Services/TextDocument/Handlers/TextDocumentHandler.cs @@ -16,6 +16,7 @@ using OmniSharp.Extensions.LanguageServer.Protocol.Models; using OmniSharp.Extensions.LanguageServer.Protocol.Server; using OmniSharp.Extensions.LanguageServer.Protocol.Server.Capabilities; +using System.Linq; namespace Microsoft.PowerShell.EditorServices.Handlers { @@ -52,7 +53,7 @@ public TextDocumentHandler( public Task Handle(DidChangeTextDocumentParams notification, CancellationToken token) { - List changedFiles = new List(); + Dictionary changedFiles = new Dictionary(); // A text change notification can batch multiple change requests foreach (TextDocumentContentChangeEvent textChange in notification.ContentChanges) @@ -64,11 +65,13 @@ public Task Handle(DidChangeTextDocumentParams notification, CancellationT textChange.Range, textChange.Text)); - changedFiles.Add(changedFile); + // Sometimes the client sends multiple changes to the same file, if so, + // grab the last one to run diagnostics on. + changedFiles[changedFile.Id] = changedFile; } // TODO: Get all recently edited files in the workspace - _analysisService.RunScriptDiagnosticsAsync(changedFiles.ToArray()); + _analysisService.RunScriptDiagnosticsAsync(changedFiles.Values.ToArray()); return Unit.Task; } diff --git a/test/PowerShellEditorServices.Test.E2E/LanguageServerProtocolMessageTests.cs b/test/PowerShellEditorServices.Test.E2E/LanguageServerProtocolMessageTests.cs index 776b84a40..bd43f06c8 100644 --- a/test/PowerShellEditorServices.Test.E2E/LanguageServerProtocolMessageTests.cs +++ b/test/PowerShellEditorServices.Test.E2E/LanguageServerProtocolMessageTests.cs @@ -145,6 +145,43 @@ public async Task CanReceiveDiagnosticsFromFileOpen() Assert.Equal("PSUseDeclaredVarsMoreThanAssignments", diagnostic.Code); } + [Fact] + public async Task CanReceiveDiagnosticsFromFileChanged() + { + string filePath = NewTestFile("$a = 4"); + await WaitForDiagnostics(); + Diagnostics.Clear(); + + LanguageClient.SendNotification("textDocument/didChange", new DidChangeTextDocumentParams + { + // Include several content changes to test against duplicate Diagnostics showing up. + ContentChanges = new Container(new [] + { + new TextDocumentContentChangeEvent + { + Text = "$a = 5" + }, + new TextDocumentContentChangeEvent + { + Text = "$a = 6" + }, + new TextDocumentContentChangeEvent + { + Text = "$a = 7" + } + }), + TextDocument = new VersionedTextDocumentIdentifier + { + Version = 4, + Uri = new Uri(filePath) + } + }); + + await WaitForDiagnostics(); + Diagnostic diagnostic = Assert.Single(Diagnostics); + Assert.Equal("PSUseDeclaredVarsMoreThanAssignments", diagnostic.Code); + } + [Fact] public async Task CanReceiveDiagnosticsFromConfigurationChange() { From a778cafff50d297517c92b3fe7222f735a87a5de Mon Sep 17 00:00:00 2001 From: Tyler Leonhardt Date: Thu, 31 Oct 2019 07:56:07 -0400 Subject: [PATCH 2/3] refactor to be simpler --- .../TextDocument/Handlers/TextDocumentHandler.cs | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/src/PowerShellEditorServices/Services/TextDocument/Handlers/TextDocumentHandler.cs b/src/PowerShellEditorServices/Services/TextDocument/Handlers/TextDocumentHandler.cs index 06075707b..d68ad98d9 100644 --- a/src/PowerShellEditorServices/Services/TextDocument/Handlers/TextDocumentHandler.cs +++ b/src/PowerShellEditorServices/Services/TextDocument/Handlers/TextDocumentHandler.cs @@ -53,25 +53,19 @@ public TextDocumentHandler( public Task Handle(DidChangeTextDocumentParams notification, CancellationToken token) { - Dictionary changedFiles = new Dictionary(); + ScriptFile changedFile = _workspaceService.GetFile(notification.TextDocument.Uri.ToString()); // A text change notification can batch multiple change requests foreach (TextDocumentContentChangeEvent textChange in notification.ContentChanges) { - ScriptFile changedFile = _workspaceService.GetFile(notification.TextDocument.Uri.ToString()); - changedFile.ApplyChange( GetFileChangeDetails( textChange.Range, textChange.Text)); - - // Sometimes the client sends multiple changes to the same file, if so, - // grab the last one to run diagnostics on. - changedFiles[changedFile.Id] = changedFile; } // TODO: Get all recently edited files in the workspace - _analysisService.RunScriptDiagnosticsAsync(changedFiles.Values.ToArray()); + _analysisService.RunScriptDiagnosticsAsync(new ScriptFile[] { changedFile }); return Unit.Task; } From d25daef15a00312ba27897f711a826f3cbaf3328 Mon Sep 17 00:00:00 2001 From: Tyler Leonhardt Date: Thu, 31 Oct 2019 08:00:23 -0400 Subject: [PATCH 3/3] remove unused imports --- .../Services/TextDocument/Handlers/TextDocumentHandler.cs | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/PowerShellEditorServices/Services/TextDocument/Handlers/TextDocumentHandler.cs b/src/PowerShellEditorServices/Services/TextDocument/Handlers/TextDocumentHandler.cs index d68ad98d9..9bf4546eb 100644 --- a/src/PowerShellEditorServices/Services/TextDocument/Handlers/TextDocumentHandler.cs +++ b/src/PowerShellEditorServices/Services/TextDocument/Handlers/TextDocumentHandler.cs @@ -4,7 +4,6 @@ // using System; -using System.Collections.Generic; using System.Threading; using System.Threading.Tasks; using Microsoft.Extensions.Logging; @@ -16,7 +15,6 @@ using OmniSharp.Extensions.LanguageServer.Protocol.Models; using OmniSharp.Extensions.LanguageServer.Protocol.Server; using OmniSharp.Extensions.LanguageServer.Protocol.Server.Capabilities; -using System.Linq; namespace Microsoft.PowerShell.EditorServices.Handlers {