Skip to content

Commit

Permalink
Send batch events for DiagnosticsUpdatedArgs
Browse files Browse the repository at this point in the history
  • Loading branch information
sharwell committed Sep 29, 2023
1 parent da79db0 commit d4fef3b
Show file tree
Hide file tree
Showing 27 changed files with 514 additions and 277 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,37 @@ public async Task<ImmutableArray<VSTypeScriptDiagnosticData>> GetPushDiagnostics
return result.SelectAsArray(data => new VSTypeScriptDiagnosticData(data));
}

[Obsolete]
public IDisposable RegisterDiagnosticsUpdatedEventHandler(Action<VSTypeScriptDiagnosticsUpdatedArgsWrapper> action)
=> new EventHandlerWrapper(_service, action);

public IDisposable RegisterDiagnosticsUpdatedEventHandler(Action<ImmutableArray<VSTypeScriptDiagnosticsUpdatedArgsWrapper>> action)
=> new EventHandlerWrapper(_service, action);

private sealed class EventHandlerWrapper : IDisposable
{
private readonly IDiagnosticService _service;
private readonly EventHandler<DiagnosticsUpdatedArgs> _handler;
private readonly EventHandler<ImmutableArray<DiagnosticsUpdatedArgs>> _handler;

[Obsolete]
internal EventHandlerWrapper(IDiagnosticService service, Action<VSTypeScriptDiagnosticsUpdatedArgsWrapper> action)
{
_service = service;
_handler = (sender, args) => action(new VSTypeScriptDiagnosticsUpdatedArgsWrapper(args));
_handler = (sender, argsCollection) =>
{
foreach (var args in argsCollection)
action(new VSTypeScriptDiagnosticsUpdatedArgsWrapper(args));
};
_service.DiagnosticsUpdated += _handler;
}

internal EventHandlerWrapper(IDiagnosticService service, Action<ImmutableArray<VSTypeScriptDiagnosticsUpdatedArgsWrapper>> action)
{
_service = service;
_handler = (sender, argsCollection) =>
{
action(ImmutableArray.CreateRange(argsCollection, static args => new VSTypeScriptDiagnosticsUpdatedArgsWrapper(args)));
};
_service.DiagnosticsUpdated += _handler;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System.Collections.Immutable;
using System.Linq;
using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.CodeAnalysis.Text;
using Microsoft.VisualStudio.Text;
Expand All @@ -15,11 +17,13 @@ private class DiagnosticsChangedEventSource(ITextBuffer subjectBuffer, IDiagnost
private readonly ITextBuffer _subjectBuffer = subjectBuffer;
private readonly IDiagnosticService _service = service;

private void OnDiagnosticsUpdated(object? sender, DiagnosticsUpdatedArgs e)
private void OnDiagnosticsUpdated(object? sender, ImmutableArray<DiagnosticsUpdatedArgs> e)
{
var documentId = e.Workspace.GetDocumentIdInCurrentContext(_subjectBuffer.AsTextContainer());
if (e.FirstOrDefault() is not { } first)
return;

if (documentId == e.DocumentId)
var documentId = first.Workspace.GetDocumentIdInCurrentContext(_subjectBuffer.AsTextContainer());
if (e.Any(static (args, expectedDocumentId) => args.DocumentId == expectedDocumentId, documentId))
{
this.RaiseChanged();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public async Task TestHasSuccessfullyLoadedBeingFalse()
// check empty since this could be called to clear up existing diagnostics
service.DiagnosticsUpdated += (s, a) =>
{
var diagnostics = a.Diagnostics;
var diagnostics = a.Single().Diagnostics;
Assert.Empty(diagnostics);
};

Expand Down Expand Up @@ -209,7 +209,7 @@ public async Task TestDisabledByDefaultAnalyzerEnabledWithEditorConfig(bool enab
var compilationDiagnostic = false;
service.DiagnosticsUpdated += (s, a) =>
{
var diagnostics = a.Diagnostics;
var diagnostics = Assert.Single(a).Diagnostics;
var diagnostic = Assert.Single(diagnostics);
Assert.Equal(DiagnosticSeverity.Warning, diagnostic.Severity);
Expand Down Expand Up @@ -262,7 +262,7 @@ private static async Task TestAnalyzerAsync(
// listen to events
service.DiagnosticsUpdated += (s, a) =>
{
var diagnostics = a.Diagnostics;
var diagnostics = a.Single().Diagnostics;
(syntax, semantic) = resultSetter(syntax, semantic, diagnostics);
};

Expand Down Expand Up @@ -305,19 +305,19 @@ public async Task TestOpenFileOnlyAnalyzerDiagnostics()
// listen to events
service.DiagnosticsUpdated += (s, a) =>
{
if (workspace.IsDocumentOpen(a.DocumentId))
if (workspace.IsDocumentOpen(a.Single().DocumentId))
{
var diagnostics = a.Diagnostics;
var diagnostics = a.Single().Diagnostics;
// check the diagnostics are reported
Assert.Equal(document.Id, a.DocumentId);
Assert.Equal(document.Id, a.Single().DocumentId);
Assert.Equal(1, diagnostics.Length);
Assert.Equal(OpenFileOnlyAnalyzer.s_syntaxRule.Id, diagnostics[0].Id);
}
if (a.DocumentId == document.Id && !workspace.IsDocumentOpen(a.DocumentId))
if (a.Single().DocumentId == document.Id && !workspace.IsDocumentOpen(a.Single().DocumentId))
{
// check the diagnostics reported are cleared
var diagnostics = a.Diagnostics;
var diagnostics = a.Single().Diagnostics;
Assert.Equal(0, diagnostics.Length);
}
};
Expand Down Expand Up @@ -376,7 +376,7 @@ public async Task TestSynchronizeWithBuild()
// listen to events
service.DiagnosticsUpdated += (s, a) =>
{
var diagnostics = a.Diagnostics;
var diagnostics = a.Single().Diagnostics;
switch (diagnostics.Length)
{
case 0:
Expand Down Expand Up @@ -495,13 +495,13 @@ public async Task TestHostAnalyzerErrorNotLeaking()
var called = false;
service.DiagnosticsUpdated += (s, e) =>
{
var diagnostics = e.Diagnostics;
var diagnostics = e.Single().Diagnostics;
if (diagnostics.Length == 0)
{
return;
}
var liveId = (LiveDiagnosticUpdateArgsId)e.Id;
var liveId = (LiveDiagnosticUpdateArgsId)e.Single().Id;
Assert.False(liveId.Analyzer is ProjectDiagnosticAnalyzer);
called = true;
Expand Down Expand Up @@ -601,13 +601,13 @@ private static async Task TestFullSolutionAnalysisForProjectAsync(AdhocWorkspace
var called = false;
service.DiagnosticsUpdated += (s, e) =>
{
var diagnostics = e.Diagnostics;
var diagnostics = e.Single().Diagnostics;
if (diagnostics.Length == 0)
{
return;
}
var liveId = (LiveDiagnosticUpdateArgsId)e.Id;
var liveId = (LiveDiagnosticUpdateArgsId)e.Single().Id;
Assert.True(liveId.Analyzer is NamedTypeAnalyzer);
called = true;
Expand Down Expand Up @@ -659,7 +659,7 @@ internal async Task TestAdditionalFileAnalyzer(bool registerFromInitialize, bool
var diagnostics = new ConcurrentSet<DiagnosticData>();
service.DiagnosticsUpdated += (s, e) =>
{
diagnostics.AddRange(e.Diagnostics);
diagnostics.AddRange(e.Single().Diagnostics);
};

var incrementalAnalyzer = (DiagnosticIncrementalAnalyzer)service.CreateIncrementalAnalyzer(workspace);
Expand Down Expand Up @@ -765,7 +765,7 @@ internal async Task TestDiagnosticSuppressor(bool includeAnalyzer, bool includeS
DiagnosticData diagnostic = null;
service.DiagnosticsUpdated += (s, e) =>
{
var diagnostics = e.Diagnostics;
var diagnostics = e.Single().Diagnostics;
if (diagnostics.Length == 0)
{
return;
Expand Down Expand Up @@ -900,7 +900,7 @@ void M()
service.DiagnosticsUpdated += (s, e) =>
{
diagnostics.AddRange(
e.Diagnostics
e.Single().Diagnostics
.Where(d => d.Id == IDEDiagnosticIds.RemoveUnnecessarySuppressionDiagnosticId)
.OrderBy(d => d.DataLocation.UnmappedFileSpan.GetClampedTextSpan(text)));
};
Expand Down Expand Up @@ -996,7 +996,7 @@ void M()
DiagnosticData diagnostic = null;
service.DiagnosticsUpdated += (s, e) =>
{
var diagnostics = e.Diagnostics;
var diagnostics = e.Single().Diagnostics;
if (diagnostics.IsEmpty)
{
return;
Expand Down Expand Up @@ -1239,11 +1239,11 @@ internal async Task TestGeneratorProducedDiagnostics(bool fullSolutionAnalysis)
var gotDiagnostics = false;
service.DiagnosticsUpdated += (s, e) =>
{
var diagnostics = e.Diagnostics;
var diagnostics = e.Single().Diagnostics;
if (diagnostics.Length == 0)
return;
var liveId = (LiveDiagnosticUpdateArgsId)e.Id;
var liveId = (LiveDiagnosticUpdateArgsId)e.Single().Id;
if (liveId.Analyzer is GeneratorDiagnosticsPlaceholderAnalyzer)
gotDiagnostics = true;
};
Expand Down
10 changes: 5 additions & 5 deletions src/EditorFeatures/Test/Diagnostics/DiagnosticServiceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ public async Task TestCleared()
var data2 = await diagnosticService.GetDiagnosticsAsync(workspace, null, null, null, includeSuppressedDiagnostics: false, CancellationToken.None);
Assert.Equal(2, data2.Count());

void MarkCalled(object sender, DiagnosticsUpdatedArgs args)
void MarkCalled(object sender, ImmutableArray<DiagnosticsUpdatedArgs> args)
{
// event is serialized. no concurrent call
if (++count == 3)
Expand All @@ -155,7 +155,7 @@ void MarkCalled(object sender, DiagnosticsUpdatedArgs args)
}
}

void MarkSet(object sender, DiagnosticsUpdatedArgs args)
void MarkSet(object sender, ImmutableArray<DiagnosticsUpdatedArgs> args)
{
mutex.Set();
}
Expand All @@ -168,7 +168,7 @@ private static DiagnosticData RaiseDiagnosticEvent(ManualResetEvent set, TestDia
var diagnostic = CreateDiagnosticData(projectId, documentId);

source.RaiseDiagnosticsUpdatedEvent(
DiagnosticsUpdatedArgs.DiagnosticsCreated(id, workspace, workspace.CurrentSolution, projectId, documentId, ImmutableArray.Create(diagnostic)));
ImmutableArray.Create(DiagnosticsUpdatedArgs.DiagnosticsCreated(id, workspace, workspace.CurrentSolution, projectId, documentId, ImmutableArray.Create(diagnostic))));

set.WaitOne();

Expand Down Expand Up @@ -203,13 +203,13 @@ public TestDiagnosticUpdateSource(bool support, DiagnosticData[]? diagnosticData
}

public bool SupportGetDiagnostics { get { return _support; } }
public event EventHandler<DiagnosticsUpdatedArgs>? DiagnosticsUpdated;
public event EventHandler<ImmutableArray<DiagnosticsUpdatedArgs>>? DiagnosticsUpdated;
public event EventHandler? DiagnosticsCleared;

public ValueTask<ImmutableArray<DiagnosticData>> GetDiagnosticsAsync(Workspace workspace, ProjectId? projectId, DocumentId? documentId, object? id, bool includeSuppressedDiagnostics = false, CancellationToken cancellationToken = default)
=> new(_support ? _diagnosticData : ImmutableArray<DiagnosticData>.Empty);

public void RaiseDiagnosticsUpdatedEvent(DiagnosticsUpdatedArgs args)
public void RaiseDiagnosticsUpdatedEvent(ImmutableArray<DiagnosticsUpdatedArgs> args)
=> DiagnosticsUpdated?.Invoke(this, args);

public void RaiseDiagnosticsClearedEvent()
Expand Down
6 changes: 3 additions & 3 deletions src/EditorFeatures/Test/Diagnostics/MockDiagnosticService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ internal class MockDiagnosticService : IDiagnosticService

private DiagnosticData? _diagnosticData;

public event EventHandler<DiagnosticsUpdatedArgs>? DiagnosticsUpdated;
public event EventHandler<ImmutableArray<DiagnosticsUpdatedArgs>>? DiagnosticsUpdated;

[ImportingConstructor]
[Obsolete(MefConstruction.ImportingConstructorMessage, error: true)]
Expand Down Expand Up @@ -65,10 +65,10 @@ internal void CreateDiagnosticAndFireEvents(Workspace workspace, MockDiagnosticA
document);

analyzerService.AddDiagnostic(_diagnosticData, diagnosticKind);
DiagnosticsUpdated?.Invoke(this, DiagnosticsUpdatedArgs.DiagnosticsCreated(
DiagnosticsUpdated?.Invoke(this, ImmutableArray.Create(DiagnosticsUpdatedArgs.DiagnosticsCreated(
this, workspace, workspace.CurrentSolution,
GetProjectId(workspace), GetDocumentId(workspace),
ImmutableArray.Create(_diagnosticData)));
ImmutableArray.Create(_diagnosticData))));
}

private static DocumentId GetDocumentId(Workspace workspace)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ void VerifyReanalyzeInvocation(ImmutableArray<DocumentId> documentIds)
var diagnosticUpdateSource = new EditAndContinueDiagnosticUpdateSource();
var emitDiagnosticsUpdated = new List<DiagnosticsUpdatedArgs>();
var emitDiagnosticsClearedCount = 0;
diagnosticUpdateSource.DiagnosticsUpdated += (object sender, DiagnosticsUpdatedArgs args) => emitDiagnosticsUpdated.Add(args);
diagnosticUpdateSource.DiagnosticsUpdated += (object sender, ImmutableArray<DiagnosticsUpdatedArgs> args) => emitDiagnosticsUpdated.AddRange(args);
diagnosticUpdateSource.DiagnosticsCleared += (object sender, EventArgs args) => emitDiagnosticsClearedCount++;

var span1 = new LinePositionSpan(new LinePosition(1, 2), new LinePosition(1, 5));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ internal static async Task<IList<ITagSpan<TTag>>> GetErrorsFromUpdateSource(Test
var analyzerServer = (MockDiagnosticAnalyzerService)workspace.GetService<IDiagnosticAnalyzerService>();
analyzerServer.AddDiagnostics(updateArgs.Diagnostics, diagnosticKind);

source.RaiseDiagnosticsUpdated(updateArgs);
source.RaiseDiagnosticsUpdated(ImmutableArray.Create(updateArgs));

await wrapper.WaitForTags();

Expand Down Expand Up @@ -78,13 +78,13 @@ private class TestDiagnosticUpdateSource : IDiagnosticUpdateSource
{
private ImmutableArray<DiagnosticData> _diagnostics = ImmutableArray<DiagnosticData>.Empty;

public void RaiseDiagnosticsUpdated(DiagnosticsUpdatedArgs args)
public void RaiseDiagnosticsUpdated(ImmutableArray<DiagnosticsUpdatedArgs> args)
{
_diagnostics = args.Diagnostics;
_diagnostics = args.SelectManyAsArray(e => e.Diagnostics);
DiagnosticsUpdated?.Invoke(this, args);
}

public event EventHandler<DiagnosticsUpdatedArgs>? DiagnosticsUpdated;
public event EventHandler<ImmutableArray<DiagnosticsUpdatedArgs>>? DiagnosticsUpdated;
public event EventHandler DiagnosticsCleared { add { } remove { } }

public bool SupportGetDiagnostics => false;
Expand Down
Loading

0 comments on commit d4fef3b

Please sign in to comment.