Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Send batch events for DiagnosticsUpdatedArgs #70039

Merged
merged 6 commits into from
Oct 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
ToddGrun marked this conversation as resolved.
Show resolved Hide resolved
{
_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,7 @@
// 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 Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.CodeAnalysis.Text;
using Microsoft.VisualStudio.Text;
Expand All @@ -15,11 +16,28 @@ 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());
var textContainer = _subjectBuffer.AsTextContainer();
var anyChanged = false;
Workspace? lastWorkspace = null;
DocumentId? documentId = null;
foreach (var args in e)
{
if (args.Workspace != lastWorkspace)
{
lastWorkspace = args.Workspace;
documentId = args.Workspace.GetDocumentIdInCurrentContext(textContainer);
}

if (args.DocumentId == documentId)
{
anyChanged = true;
break;
}
}

if (documentId == e.DocumentId)
if (anyChanged)
{
this.RaiseChanged();
}
Expand Down
Loading