Skip to content

Commit

Permalink
Merge base and parent type in diagnostic subsystem (#77110)
Browse files Browse the repository at this point in the history
  • Loading branch information
CyrusNajmabadi authored Feb 7, 2025
2 parents 459efff + 66139ef commit 1061ea0
Showing 1 changed file with 19 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,33 +18,35 @@ internal partial class DiagnosticAnalyzerService
private partial class DiagnosticIncrementalAnalyzer
{
public Task<ImmutableArray<DiagnosticData>> GetDiagnosticsForIdsAsync(Solution solution, ProjectId projectId, DocumentId? documentId, ImmutableHashSet<string>? diagnosticIds, Func<DiagnosticAnalyzer, bool>? shouldIncludeAnalyzer, bool includeLocalDocumentDiagnostics, bool includeNonLocalDocumentDiagnostics, CancellationToken cancellationToken)
=> new IdeLatestDiagnosticGetter(this, solution, projectId, documentId, diagnosticIds, shouldIncludeAnalyzer, includeLocalDocumentDiagnostics, includeNonLocalDocumentDiagnostics).GetDiagnosticsAsync(cancellationToken);
=> new DiagnosticGetter(this, solution, projectId, documentId, diagnosticIds, shouldIncludeAnalyzer, includeLocalDocumentDiagnostics, includeNonLocalDocumentDiagnostics).GetDiagnosticsAsync(cancellationToken);

public Task<ImmutableArray<DiagnosticData>> GetProjectDiagnosticsForIdsAsync(Solution solution, ProjectId projectId, ImmutableHashSet<string>? diagnosticIds, Func<DiagnosticAnalyzer, bool>? shouldIncludeAnalyzer, bool includeNonLocalDocumentDiagnostics, CancellationToken cancellationToken)
=> new IdeLatestDiagnosticGetter(this, solution, projectId, documentId: null, diagnosticIds, shouldIncludeAnalyzer, includeLocalDocumentDiagnostics: false, includeNonLocalDocumentDiagnostics).GetProjectDiagnosticsAsync(cancellationToken);
=> new DiagnosticGetter(this, solution, projectId, documentId: null, diagnosticIds, shouldIncludeAnalyzer, includeLocalDocumentDiagnostics: false, includeNonLocalDocumentDiagnostics).GetProjectDiagnosticsAsync(cancellationToken);

private abstract class DiagnosticGetter(
private sealed class DiagnosticGetter(
DiagnosticIncrementalAnalyzer owner,
Solution solution,
ProjectId projectId,
DocumentId? documentId,
ImmutableHashSet<string>? diagnosticIds,
Func<DiagnosticAnalyzer, bool>? shouldIncludeAnalyzer,
bool includeLocalDocumentDiagnostics,
bool includeNonLocalDocumentDiagnostics)
{
protected readonly DiagnosticIncrementalAnalyzer Owner = owner;

protected readonly Solution Solution = solution;
protected readonly ProjectId ProjectId = projectId;
protected readonly DocumentId? DocumentId = documentId;
protected readonly bool IncludeLocalDocumentDiagnostics = includeLocalDocumentDiagnostics;
protected readonly bool IncludeNonLocalDocumentDiagnostics = includeNonLocalDocumentDiagnostics;
private readonly DiagnosticIncrementalAnalyzer Owner = owner;

protected StateManager StateManager => Owner._stateManager;
private readonly Solution Solution = solution;
private readonly ProjectId ProjectId = projectId;
private readonly DocumentId? DocumentId = documentId;
private readonly ImmutableHashSet<string>? _diagnosticIds = diagnosticIds;
private readonly Func<DiagnosticAnalyzer, bool>? _shouldIncludeAnalyzer = shouldIncludeAnalyzer;
private readonly bool IncludeLocalDocumentDiagnostics = includeLocalDocumentDiagnostics;
private readonly bool IncludeNonLocalDocumentDiagnostics = includeNonLocalDocumentDiagnostics;

protected virtual bool ShouldIncludeDiagnostic(DiagnosticData diagnostic) => true;
private StateManager StateManager => Owner._stateManager;

protected abstract Task ProduceDiagnosticsAsync(
Project project, IReadOnlyList<DocumentId> documentIds, bool includeProjectNonLocalResult, ArrayBuilder<DiagnosticData> builder, CancellationToken cancellationToken);
private bool ShouldIncludeDiagnostic(DiagnosticData diagnostic)
=> _diagnosticIds == null || _diagnosticIds.Contains(diagnostic.Id);

public async Task<ImmutableArray<DiagnosticData>> GetDiagnosticsAsync(CancellationToken cancellationToken)
{
Expand All @@ -62,7 +64,7 @@ public async Task<ImmutableArray<DiagnosticData>> GetDiagnosticsAsync(Cancellati
includeProjectNonLocalResult, cancellationToken).ConfigureAwait(false);
}

protected async Task<ImmutableArray<DiagnosticData>> ProduceProjectDiagnosticsAsync(
private async Task<ImmutableArray<DiagnosticData>> ProduceProjectDiagnosticsAsync(
Project project, IReadOnlyList<DocumentId> documentIds,
bool includeProjectNonLocalResult, CancellationToken cancellationToken)
{
Expand All @@ -72,30 +74,14 @@ await this.ProduceDiagnosticsAsync(
return builder.ToImmutableAndClear();
}

protected void AddIncludedDiagnostics(ArrayBuilder<DiagnosticData> builder, ImmutableArray<DiagnosticData> diagnostics)
private void AddIncludedDiagnostics(ArrayBuilder<DiagnosticData> builder, ImmutableArray<DiagnosticData> diagnostics)
{
foreach (var diagnostic in diagnostics)
{
if (ShouldIncludeDiagnostic(diagnostic))
builder.Add(diagnostic);
}
}
}

private sealed class IdeLatestDiagnosticGetter(
DiagnosticIncrementalAnalyzer owner,
Solution solution,
ProjectId projectId,
DocumentId? documentId,
ImmutableHashSet<string>? diagnosticIds,
Func<DiagnosticAnalyzer, bool>? shouldIncludeAnalyzer,
bool includeLocalDocumentDiagnostics,
bool includeNonLocalDocumentDiagnostics)
: DiagnosticGetter(
owner, solution, projectId, documentId, includeLocalDocumentDiagnostics, includeNonLocalDocumentDiagnostics)
{
private readonly ImmutableHashSet<string>? _diagnosticIds = diagnosticIds;
private readonly Func<DiagnosticAnalyzer, bool>? _shouldIncludeAnalyzer = shouldIncludeAnalyzer;

public async Task<ImmutableArray<DiagnosticData>> GetProjectDiagnosticsAsync(CancellationToken cancellationToken)
{
Expand All @@ -107,10 +93,7 @@ public async Task<ImmutableArray<DiagnosticData>> GetProjectDiagnosticsAsync(Can
project, documentIds: [], includeProjectNonLocalResult: true, cancellationToken).ConfigureAwait(false);
}

protected override bool ShouldIncludeDiagnostic(DiagnosticData diagnostic)
=> _diagnosticIds == null || _diagnosticIds.Contains(diagnostic.Id);

protected override async Task ProduceDiagnosticsAsync(
private async Task ProduceDiagnosticsAsync(
Project project,
IReadOnlyList<DocumentId> documentIds,
bool includeProjectNonLocalResult,
Expand Down

0 comments on commit 1061ea0

Please sign in to comment.