Skip to content

Commit 2a7e8b6

Browse files
TylerLeonhardtrjmholt
authored andcommitted
Fix duplicate diagnostics caused by DidChange handler (#1079)
1 parent 259fe9a commit 2a7e8b6

File tree

2 files changed

+39
-7
lines changed

2 files changed

+39
-7
lines changed

src/PowerShellEditorServices/Services/TextDocument/Handlers/TextDocumentHandler.cs

+2-7
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
//
55

66
using System;
7-
using System.Collections.Generic;
87
using System.Threading;
98
using System.Threading.Tasks;
109
using Microsoft.Extensions.Logging;
@@ -52,23 +51,19 @@ public TextDocumentHandler(
5251

5352
public Task<Unit> Handle(DidChangeTextDocumentParams notification, CancellationToken token)
5453
{
55-
List<ScriptFile> changedFiles = new List<ScriptFile>();
54+
ScriptFile changedFile = _workspaceService.GetFile(notification.TextDocument.Uri.ToString());
5655

5756
// A text change notification can batch multiple change requests
5857
foreach (TextDocumentContentChangeEvent textChange in notification.ContentChanges)
5958
{
60-
ScriptFile changedFile = _workspaceService.GetFile(notification.TextDocument.Uri.ToString());
61-
6259
changedFile.ApplyChange(
6360
GetFileChangeDetails(
6461
textChange.Range,
6562
textChange.Text));
66-
67-
changedFiles.Add(changedFile);
6863
}
6964

7065
// TODO: Get all recently edited files in the workspace
71-
_analysisService.RunScriptDiagnosticsAsync(changedFiles.ToArray());
66+
_analysisService.RunScriptDiagnosticsAsync(new ScriptFile[] { changedFile });
7267
return Unit.Task;
7368
}
7469

test/PowerShellEditorServices.Test.E2E/LanguageServerProtocolMessageTests.cs

+37
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,43 @@ public async Task CanReceiveDiagnosticsFromFileOpen()
145145
Assert.Equal("PSUseDeclaredVarsMoreThanAssignments", diagnostic.Code);
146146
}
147147

148+
[Fact]
149+
public async Task CanReceiveDiagnosticsFromFileChanged()
150+
{
151+
string filePath = NewTestFile("$a = 4");
152+
await WaitForDiagnostics();
153+
Diagnostics.Clear();
154+
155+
LanguageClient.SendNotification("textDocument/didChange", new DidChangeTextDocumentParams
156+
{
157+
// Include several content changes to test against duplicate Diagnostics showing up.
158+
ContentChanges = new Container<TextDocumentContentChangeEvent>(new []
159+
{
160+
new TextDocumentContentChangeEvent
161+
{
162+
Text = "$a = 5"
163+
},
164+
new TextDocumentContentChangeEvent
165+
{
166+
Text = "$a = 6"
167+
},
168+
new TextDocumentContentChangeEvent
169+
{
170+
Text = "$a = 7"
171+
}
172+
}),
173+
TextDocument = new VersionedTextDocumentIdentifier
174+
{
175+
Version = 4,
176+
Uri = new Uri(filePath)
177+
}
178+
});
179+
180+
await WaitForDiagnostics();
181+
Diagnostic diagnostic = Assert.Single(Diagnostics);
182+
Assert.Equal("PSUseDeclaredVarsMoreThanAssignments", diagnostic.Code);
183+
}
184+
148185
[Fact]
149186
public async Task CanReceiveDiagnosticsFromConfigurationChange()
150187
{

0 commit comments

Comments
 (0)