diff --git a/src/PowerShellEditorServices/Services/TextDocument/Handlers/TextDocumentHandler.cs b/src/PowerShellEditorServices/Services/TextDocument/Handlers/TextDocumentHandler.cs index 7239fbe6a..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; @@ -52,23 +51,19 @@ public TextDocumentHandler( public Task Handle(DidChangeTextDocumentParams notification, CancellationToken token) { - List changedFiles = new List(); + 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)); - - changedFiles.Add(changedFile); } // TODO: Get all recently edited files in the workspace - _analysisService.RunScriptDiagnosticsAsync(changedFiles.ToArray()); + _analysisService.RunScriptDiagnosticsAsync(new ScriptFile[] { changedFile }); 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() {