Skip to content

Commit

Permalink
Merge pull request #72445 from CyrusNajmabadi/removeDiagFunctionality
Browse files Browse the repository at this point in the history
  • Loading branch information
CyrusNajmabadi authored Mar 8, 2024
2 parents 191840b + 9edd4e8 commit c759290
Show file tree
Hide file tree
Showing 17 changed files with 6 additions and 507 deletions.
107 changes: 4 additions & 103 deletions src/EditorFeatures/Test/Diagnostics/DiagnosticServiceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,86 +26,7 @@ private static DiagnosticService GetDiagnosticService(TestWorkspace workspace)
}

[Fact]
public async Task TestGetDiagnostics1()
{
using var workspace = new TestWorkspace(composition: EditorTestCompositions.EditorFeatures);
var mutex = new ManualResetEvent(false);
var document = workspace.CurrentSolution.AddProject("TestProject", "TestProject", LanguageNames.CSharp).AddDocument("TestDocument", string.Empty);

var source = new TestDiagnosticUpdateSource(false, null);
var diagnosticService = GetDiagnosticService(workspace);
diagnosticService.Register(source);

diagnosticService.DiagnosticsUpdated += (s, o) =>
{
foreach (var _ in o)
mutex.Set();
};

var id = Tuple.Create(workspace, document);
var diagnostic = RaiseDiagnosticEvent(mutex, source, workspace, document.Project.Id, document.Id, id);

var data1 = await diagnosticService.GetDiagnosticsAsync(workspace, null, null, null, includeSuppressedDiagnostics: false, CancellationToken.None);
Assert.Equal(diagnostic, data1.Single());

var data2 = await diagnosticService.GetDiagnosticsAsync(workspace, document.Project.Id, null, null, includeSuppressedDiagnostics: false, CancellationToken.None);
Assert.Equal(diagnostic, data2.Single());

var data3 = await diagnosticService.GetDiagnosticsAsync(workspace, document.Project.Id, document.Id, null, includeSuppressedDiagnostics: false, CancellationToken.None);
Assert.Equal(diagnostic, data3.Single());

var data4 = await diagnosticService.GetDiagnosticsAsync(workspace, document.Project.Id, document.Id, id, includeSuppressedDiagnostics: false, CancellationToken.None);
Assert.Equal(diagnostic, data4.Single());
}

[Fact]
public async Task TestGetDiagnostics2()
{
using var workspace = new TestWorkspace(composition: EditorTestCompositions.EditorFeatures);
var mutex = new ManualResetEvent(false);
var document = workspace.CurrentSolution.AddProject("TestProject", "TestProject", LanguageNames.CSharp).AddDocument("TestDocument", string.Empty);
var document2 = document.Project.AddDocument("TestDocument2", string.Empty);

var source = new TestDiagnosticUpdateSource(false, null);
var diagnosticService = GetDiagnosticService(workspace);
diagnosticService.Register(source);

diagnosticService.DiagnosticsUpdated += (s, o) =>
{
foreach (var _ in o)
mutex.Set();
};

var id = Tuple.Create(workspace, document);
RaiseDiagnosticEvent(mutex, source, workspace, document.Project.Id, document.Id, id);

var id2 = Tuple.Create(workspace, document.Project, document);
RaiseDiagnosticEvent(mutex, source, workspace, document.Project.Id, document.Id, id2);

RaiseDiagnosticEvent(mutex, source, workspace, document2.Project.Id, document2.Id, Tuple.Create(workspace, document2));

var id3 = Tuple.Create(workspace, document.Project);
RaiseDiagnosticEvent(mutex, source, workspace, document.Project.Id, null, id3);
RaiseDiagnosticEvent(mutex, source, workspace, null, null, Tuple.Create(workspace));

var data1 = await diagnosticService.GetDiagnosticsAsync(workspace, null, null, null, includeSuppressedDiagnostics: false, CancellationToken.None);
Assert.Equal(5, data1.Count());

var data2 = await diagnosticService.GetDiagnosticsAsync(workspace, document.Project.Id, null, null, includeSuppressedDiagnostics: false, CancellationToken.None);
Assert.Equal(4, data2.Count());

var data3 = await diagnosticService.GetDiagnosticsAsync(workspace, document.Project.Id, null, id3, includeSuppressedDiagnostics: false, CancellationToken.None);
Assert.Equal(1, data3.Count());

var data4 = await diagnosticService.GetDiagnosticsAsync(workspace, document.Project.Id, document.Id, null, includeSuppressedDiagnostics: false, CancellationToken.None);
Assert.Equal(2, data4.Count());

var data5 = await diagnosticService.GetDiagnosticsAsync(workspace, document.Project.Id, document.Id, id, includeSuppressedDiagnostics: false, CancellationToken.None);
Assert.Equal(1, data5.Count());
}

[Fact]
public async Task TestCleared()
public void TestCleared()
{
using var workspace = new TestWorkspace(composition: EditorTestCompositions.EditorFeatures);
var mutex = new ManualResetEvent(false);
Expand All @@ -114,10 +35,10 @@ public async Task TestCleared()

var diagnosticService = GetDiagnosticService(workspace);

var source1 = new TestDiagnosticUpdateSource(support: false, diagnosticData: null);
var source1 = new TestDiagnosticUpdateSource();
diagnosticService.Register(source1);

var source2 = new TestDiagnosticUpdateSource(support: false, diagnosticData: null);
var source2 = new TestDiagnosticUpdateSource();
diagnosticService.Register(source2);

diagnosticService.DiagnosticsUpdated += MarkSet;
Expand All @@ -130,10 +51,6 @@ public async Task TestCleared()
RaiseDiagnosticEvent(mutex, source2, workspace, document.Project.Id, null, Tuple.Create(workspace, document.Project));
RaiseDiagnosticEvent(mutex, source2, workspace, null, null, Tuple.Create(workspace));

// confirm data is there.
var data1 = await diagnosticService.GetDiagnosticsAsync(workspace, null, null, null, includeSuppressedDiagnostics: false, CancellationToken.None);
Assert.Equal(5, data1.Count());

diagnosticService.DiagnosticsUpdated -= MarkSet;

// confirm clear for a source
Expand All @@ -144,10 +61,7 @@ public async Task TestCleared()
source1.RaiseDiagnosticsClearedEvent();

mutex.WaitOne();

// confirm there are 2 data left
var data2 = await diagnosticService.GetDiagnosticsAsync(workspace, null, null, null, includeSuppressedDiagnostics: false, CancellationToken.None);
Assert.Equal(2, data2.Count());
return;

void MarkCalled(object sender, ImmutableArray<DiagnosticsUpdatedArgs> args)
{
Expand Down Expand Up @@ -200,22 +114,9 @@ private static DiagnosticData CreateDiagnosticData(ProjectId? projectId, Documen

private class TestDiagnosticUpdateSource : IDiagnosticUpdateSource
{
private readonly bool _support;
private readonly ImmutableArray<DiagnosticData> _diagnosticData;

public TestDiagnosticUpdateSource(bool support, DiagnosticData[]? diagnosticData)
{
_support = support;
_diagnosticData = (diagnosticData ?? []).ToImmutableArray();
}

public bool SupportGetDiagnostics { get { return _support; } }
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(ImmutableArray<DiagnosticsUpdatedArgs> args)
=> DiagnosticsUpdated?.Invoke(this, args);

Expand Down
54 changes: 1 addition & 53 deletions src/EditorFeatures/Test/Diagnostics/MockDiagnosticService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,8 @@
using System.Collections.Immutable;
using System.Composition;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.CodeAnalysis.EditAndContinue.UnitTests;
using Microsoft.CodeAnalysis.Host.Mef;
using Microsoft.CodeAnalysis.Options;
using Roslyn.Utilities;
using Xunit;

namespace Microsoft.CodeAnalysis.Editor.UnitTests.Diagnostics
Expand All @@ -22,59 +17,12 @@ internal class MockDiagnosticService : IDiagnosticService
{
public const string DiagnosticId = "MockId";

private DiagnosticData? _diagnosticData;

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

[ImportingConstructor]
[Obsolete(MefConstruction.ImportingConstructorMessage, error: true)]
public MockDiagnosticService()
{
}

public ValueTask<ImmutableArray<DiagnosticData>> GetDiagnosticsAsync(
Workspace workspace, ProjectId? projectId, DocumentId? documentId, object? id, bool includeSuppressedDiagnostics, CancellationToken cancellationToken)
{
return new ValueTask<ImmutableArray<DiagnosticData>>(GetDiagnostics(workspace, projectId, documentId));
}

private ImmutableArray<DiagnosticData> GetDiagnostics(Workspace workspace, ProjectId? projectId, DocumentId? documentId)
{
Assert.Equal(projectId, GetProjectId(workspace));
Assert.Equal(documentId, GetDocumentId(workspace));

return _diagnosticData == null ? ImmutableArray<DiagnosticData>.Empty : ImmutableArray.Create(_diagnosticData);
}

public ImmutableArray<DiagnosticBucket> GetDiagnosticBuckets(
Workspace workspace, ProjectId? projectId, DocumentId? documentId, CancellationToken cancellationToken)
{
Assert.Equal(projectId, GetProjectId(workspace));
Assert.Equal(documentId, GetDocumentId(workspace));

return _diagnosticData == null
? ImmutableArray<DiagnosticBucket>.Empty
: ImmutableArray.Create(new DiagnosticBucket(this, workspace, GetProjectId(workspace), GetDocumentId(workspace)));
}

internal void CreateDiagnosticAndFireEvents(Workspace workspace, MockDiagnosticAnalyzerService analyzerService, Location location, DiagnosticKind diagnosticKind, bool isSuppressed)
{
var document = workspace.CurrentSolution.Projects.Single().Documents.Single();
_diagnosticData = DiagnosticData.Create(Diagnostic.Create(DiagnosticId, "MockCategory", "MockMessage", DiagnosticSeverity.Error, DiagnosticSeverity.Error, isEnabledByDefault: true, warningLevel: 0, isSuppressed: isSuppressed,
location: location),
document);

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

private static DocumentId GetDocumentId(Workspace workspace)
=> workspace.CurrentSolution.Projects.Single().Documents.Single().Id;

private static ProjectId GetProjectId(Workspace workspace)
=> workspace.CurrentSolution.Projects.Single().Id;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,6 @@ public Task<ImmutableArray<DiagnosticData>> GetDiagnosticsForSpanAsync(TextDocum
public Task<ImmutableArray<DiagnosticData>> GetProjectDiagnosticsForIdsAsync(Solution solution, ProjectId? projectId, ImmutableHashSet<string>? diagnosticIds, Func<DiagnosticAnalyzer, bool>? shouldIncludeAnalyzer, bool includeSuppressedDiagnostics, bool includeNonLocalDocumentDiagnostics, CancellationToken cancellationToken)
=> throw new NotImplementedException();

public Task<ImmutableArray<DiagnosticData>> GetSpecificCachedDiagnosticsAsync(Workspace workspace, object id, bool includeSuppressedDiagnostics, bool includeNonLocalDocumentDiagnostics, CancellationToken cancellationToken)
=> throw new NotImplementedException();

public Task<(ImmutableArray<DiagnosticData> diagnostics, bool upToDate)> TryGetDiagnosticsForSpanAsync(TextDocument document, TextSpan range, Func<string, bool>? shouldIncludeDiagnostic, bool includeSuppressedDiagnostics, ICodeActionRequestPriorityProvider priorityProvider, DiagnosticKind diagnosticKind, bool isExplicit, CancellationToken cancellationToken)
=> throw new NotImplementedException();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,20 +75,12 @@ internal static DiagnosticData CreateDiagnosticData(EditorTestHostDocument docum

private class TestDiagnosticUpdateSource : IDiagnosticUpdateSource
{
private ImmutableArray<DiagnosticData> _diagnostics = ImmutableArray<DiagnosticData>.Empty;

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

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

public bool SupportGetDiagnostics => false;

public ValueTask<ImmutableArray<DiagnosticData>> GetDiagnosticsAsync(Workspace workspace, ProjectId? projectId, DocumentId? documentId, object? id, bool includeSuppressedDiagnostics = false, CancellationToken cancellationToken = default)
=> new(includeSuppressedDiagnostics ? _diagnostics : _diagnostics.WhereAsArray(d => !d.IsSuppressed));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,6 @@ internal abstract class AbstractHostDiagnosticUpdateSource : IDiagnosticUpdateSo

public abstract Workspace Workspace { get; }

public bool SupportGetDiagnostics => false;

public ValueTask<ImmutableArray<DiagnosticData>> GetDiagnosticsAsync(Workspace workspace, ProjectId? projectId, DocumentId? documentId, object? id, bool includeSuppressedDiagnostics, CancellationToken cancellationToken)
=> new([]);

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

Expand Down
28 changes: 0 additions & 28 deletions src/Features/Core/Portable/Diagnostics/DiagnosticBucket.cs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -30,24 +30,6 @@ internal interface IDiagnosticAnalyzerService
/// </summary>
void Reanalyze(Workspace workspace, IEnumerable<ProjectId>? projectIds, IEnumerable<DocumentId>? documentIds, bool highPriority);

/// <summary>
/// Get specific diagnostics currently stored in the source. returned diagnostic might be out-of-date if solution has changed but analyzer hasn't run for the new solution.
/// </summary>
/// <param name="workspace">Workspace to fetch the diagnostics for.</param>
/// <param name="id">
/// Specific id to scope the returned diagnostics. This id must correspond to the
/// <see cref="Common.UpdatedEventArgs.Id"/> associated with <see cref="DiagnosticsUpdatedArgs"/> event.
/// </param>
/// <param name="includeSuppressedDiagnostics">Indicates if diagnostics suppressed in source via pragmas and SuppressMessageAttributes should be returned.</param>
/// <param name="includeNonLocalDocumentDiagnostics">
/// Indicates if non-local document diagnostics must be returned.
/// Non-local diagnostics are the ones reported by analyzers either at compilation end callback OR
/// in a different file from which the callback was made. Entire project must be analyzed to get the
/// complete set of non-local document diagnostics.
/// </param>
/// <param name="cancellationToken">Cancellation token.</param>
Task<ImmutableArray<DiagnosticData>> GetSpecificCachedDiagnosticsAsync(Workspace workspace, object id, bool includeSuppressedDiagnostics, bool includeNonLocalDocumentDiagnostics, CancellationToken cancellationToken);

/// <summary>
/// Get diagnostics currently stored in the source. returned diagnostic might be out-of-date if solution has changed but analyzer hasn't run for the new solution.
/// </summary>
Expand Down
11 changes: 0 additions & 11 deletions src/Features/Core/Portable/Diagnostics/IDiagnosticUpdateSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,4 @@ internal interface IDiagnosticUpdateSource
/// Raise this when all diagnostics reported from this update source has cleared
/// </summary>
event EventHandler DiagnosticsCleared;

/// <summary>
/// Return <see langword="true"/> if the source supports <see cref="GetDiagnosticsAsync"/> API otherwise, return
/// <see langword="false"/> so that the engine can cache data from <see cref="DiagnosticsUpdated"/> in memory.
/// </summary>
bool SupportGetDiagnostics { get; }

/// <summary>
/// Get diagnostics stored in the source.
/// </summary>
ValueTask<ImmutableArray<DiagnosticData>> GetDiagnosticsAsync(Workspace workspace, ProjectId? projectId, DocumentId? documentId, object? id, bool includeSuppressedDiagnostics, CancellationToken cancellationToken);
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,6 @@ internal EditAndContinueDiagnosticUpdateSource()
public event EventHandler<ImmutableArray<DiagnosticsUpdatedArgs>>? DiagnosticsUpdated;
public event EventHandler? DiagnosticsCleared;

/// <summary>
/// This implementation reports diagnostics via <see cref="DiagnosticsUpdated"/> event.
/// </summary>
public bool SupportGetDiagnostics => false;

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

/// <summary>
/// Clears all diagnostics reported thru this source.
/// We do not track the particular reported diagnostics here since we can just clear all of them at once.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,16 +148,6 @@ public Task<ImmutableArray<DiagnosticData>> GetCachedDiagnosticsAsync(Workspace
return SpecializedTasks.EmptyImmutableArray<DiagnosticData>();
}

public Task<ImmutableArray<DiagnosticData>> GetSpecificCachedDiagnosticsAsync(Workspace workspace, object id, bool includeSuppressedDiagnostics, bool includeNonLocalDocumentDiagnostics, CancellationToken cancellationToken)
{
if (_map.TryGetValue(workspace, out var analyzer))
{
return analyzer.GetSpecificCachedDiagnosticsAsync(workspace.CurrentSolution, id, includeSuppressedDiagnostics, includeNonLocalDocumentDiagnostics, cancellationToken);
}

return SpecializedTasks.EmptyImmutableArray<DiagnosticData>();
}

public Task<ImmutableArray<DiagnosticData>> GetDiagnosticsAsync(Solution solution, ProjectId? projectId, DocumentId? documentId, bool includeSuppressedDiagnostics, bool includeNonLocalDocumentDiagnostics, CancellationToken cancellationToken)
{
if (_map.TryGetValue(solution.Workspace, out var analyzer))
Expand Down
Loading

0 comments on commit c759290

Please sign in to comment.