Skip to content

Commit

Permalink
Add AdditionalTextProvider (#68092)
Browse files Browse the repository at this point in the history
  • Loading branch information
CyrusNajmabadi authored May 9, 2023
1 parent b2fbfc5 commit e6655da
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Licensed to the .NET Foundation under one or more agreements.
// 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;
using System.Collections.Generic;

namespace Microsoft.CodeAnalysis.Diagnostics
{
/// <summary>
/// Provides custom values associated with <see cref="AdditionalText"/> instances using the given computeValue delegate.
/// </summary>
public sealed class AdditionalTextValueProvider<TValue>
{
internal readonly AnalysisValueProvider<AdditionalText, TValue> CoreValueProvider;

/// <summary>
/// Provides custom values associated with <see cref="AdditionalText"/> instances using the given <paramref name="computeValue"/>.
/// </summary>
/// <param name="computeValue">Delegate to compute the value associated with a given <see cref="AdditionalText"/> instance.</param>
/// <param name="additionalTextComparer">Optional equality comparer to determine equivalent <see cref="AdditionalText"/> instances that have the same value.
/// If no comparer is provided, then <see cref="EqualityComparer{T}.Default"/> is used by default.</param>
public AdditionalTextValueProvider(Func<AdditionalText, TValue> computeValue, IEqualityComparer<AdditionalText>? additionalTextComparer = null)
{
CoreValueProvider = new AnalysisValueProvider<AdditionalText, TValue>(computeValue, additionalTextComparer ?? EqualityComparer<AdditionalText>.Default);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,21 @@ public bool TryGetValue<TValue>(SourceText text, SourceTextValueProvider<TValue>
return TryGetValue(text, valueProvider.CoreValueProvider, out value);
}

/// <summary>
/// Attempts to compute or get the cached value provided by the given <paramref name="valueProvider"/> for the given <paramref name="text"/>.
/// Note that the pair {<paramref name="valueProvider"/>, <paramref name="text"/>} acts as the key.
/// Reusing the same <paramref name="valueProvider"/> instance across analyzer actions and/or analyzer instances can improve the overall analyzer performance by avoiding recomputation of the values.
/// </summary>
/// <typeparam name="TValue">The type of the value associated with the key.</typeparam>
/// <param name="text"><see cref="AdditionalText"/> for which the value is queried.</param>
/// <param name="valueProvider">Provider that computes the underlying value.</param>
/// <param name="value">Value associated with the key.</param>
/// <returns>Returns true on success, false otherwise.</returns>
public bool TryGetValue<TValue>(AdditionalText text, AdditionalTextValueProvider<TValue> valueProvider, [MaybeNullWhen(false)] out TValue value)
{
return TryGetValue(text, valueProvider.CoreValueProvider, out value);
}

private bool TryGetValue<TKey, TValue>(TKey key, AnalysisValueProvider<TKey, TValue> valueProvider, [MaybeNullWhen(false)] out TValue value)
where TKey : class
{
Expand Down Expand Up @@ -485,6 +500,21 @@ public bool TryGetValue<TValue>(SourceText text, SourceTextValueProvider<TValue>
return TryGetValue(text, valueProvider.CoreValueProvider, out value);
}

/// <summary>
/// Attempts to compute or get the cached value provided by the given <paramref name="valueProvider"/> for the given <paramref name="text"/>.
/// Note that the pair {<paramref name="valueProvider"/>, <paramref name="text"/>} acts as the key.
/// Reusing the same <paramref name="valueProvider"/> instance across analyzer actions and/or analyzer instances can improve the overall analyzer performance by avoiding recomputation of the values.
/// </summary>
/// <typeparam name="TValue">The type of the value associated with the key.</typeparam>
/// <param name="text"><see cref="AdditionalText"/> instance for which the value is queried.</param>
/// <param name="valueProvider">Provider that computes the underlying value.</param>
/// <param name="value">Value associated with the key.</param>
/// <returns>Returns true on success, false otherwise.</returns>
public bool TryGetValue<TValue>(AdditionalText text, AdditionalTextValueProvider<TValue> valueProvider, [MaybeNullWhen(false)] out TValue value)
{
return TryGetValue(text, valueProvider.CoreValueProvider, out value);
}

/// <summary>
/// Attempts to compute or get the cached value provided by the given <paramref name="valueProvider"/> for the given <paramref name="tree"/>.
/// Note that the pair {<paramref name="valueProvider"/>, <paramref name="tree"/>} acts as the key.
Expand Down Expand Up @@ -592,6 +622,21 @@ public bool TryGetValue<TValue>(SourceText text, SourceTextValueProvider<TValue>
return TryGetValue(text, valueProvider.CoreValueProvider, out value);
}

/// <summary>
/// Attempts to compute or get the cached value provided by the given <paramref name="valueProvider"/> for the given <paramref name="text"/>.
/// Note that the pair {<paramref name="valueProvider"/>, <paramref name="text"/>} acts as the key.
/// Reusing the same <paramref name="valueProvider"/> instance across analyzer actions and/or analyzer instances can improve the overall analyzer performance by avoiding recomputation of the values.
/// </summary>
/// <typeparam name="TValue">The type of the value associated with the key.</typeparam>
/// <param name="text"><see cref="AdditionalText"/> for which the value is queried.</param>
/// <param name="valueProvider">Provider that computes the underlying value.</param>
/// <param name="value">Value associated with the key.</param>
/// <returns>Returns true on success, false otherwise.</returns>
public bool TryGetValue<TValue>(AdditionalText text, AdditionalTextValueProvider<TValue> valueProvider, [MaybeNullWhen(false)] out TValue value)
{
return TryGetValue(text, valueProvider.CoreValueProvider, out value);
}

/// <summary>
/// Attempts to compute or get the cached value provided by the given <paramref name="valueProvider"/> for the given <paramref name="tree"/>.
/// Note that the pair {<paramref name="valueProvider"/>, <paramref name="tree"/>} acts as the key.
Expand Down
5 changes: 5 additions & 0 deletions src/Compilers/Core/Portable/PublicAPI.Unshipped.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
*REMOVED*static Microsoft.CodeAnalysis.SeparatedSyntaxList<TNode>.implicit operator Microsoft.CodeAnalysis.SeparatedSyntaxList<TNode!>(Microsoft.CodeAnalysis.SeparatedSyntaxList<Microsoft.CodeAnalysis.SyntaxNode!> nodes) -> Microsoft.CodeAnalysis.SeparatedSyntaxList<TNode!>
*REMOVED*static Microsoft.CodeAnalysis.SyntaxList<TNode>.implicit operator Microsoft.CodeAnalysis.SyntaxList<TNode!>(Microsoft.CodeAnalysis.SyntaxList<Microsoft.CodeAnalysis.SyntaxNode!> nodes) -> Microsoft.CodeAnalysis.SyntaxList<TNode!>
Microsoft.CodeAnalysis.Compilation.SupportsRuntimeCapability(Microsoft.CodeAnalysis.RuntimeCapability capability) -> bool
Microsoft.CodeAnalysis.Diagnostics.AdditionalTextValueProvider<TValue>
Microsoft.CodeAnalysis.Diagnostics.AdditionalTextValueProvider<TValue>.AdditionalTextValueProvider(System.Func<Microsoft.CodeAnalysis.AdditionalText!, TValue>! computeValue, System.Collections.Generic.IEqualityComparer<Microsoft.CodeAnalysis.AdditionalText!>? additionalTextComparer = null) -> void
Microsoft.CodeAnalysis.Diagnostics.AnalysisContext.TryGetValue<TValue>(Microsoft.CodeAnalysis.AdditionalText! text, Microsoft.CodeAnalysis.Diagnostics.AdditionalTextValueProvider<TValue>! valueProvider, out TValue value) -> bool
Microsoft.CodeAnalysis.Diagnostics.CompilationAnalysisContext.TryGetValue<TValue>(Microsoft.CodeAnalysis.AdditionalText! text, Microsoft.CodeAnalysis.Diagnostics.AdditionalTextValueProvider<TValue>! valueProvider, out TValue value) -> bool
Microsoft.CodeAnalysis.Diagnostics.CompilationStartAnalysisContext.TryGetValue<TValue>(Microsoft.CodeAnalysis.AdditionalText! text, Microsoft.CodeAnalysis.Diagnostics.AdditionalTextValueProvider<TValue>! valueProvider, out TValue value) -> bool
Microsoft.CodeAnalysis.Diagnostics.CompilationWithAnalyzers.GetAnalysisResultAsync(Microsoft.CodeAnalysis.SyntaxTree! tree, Microsoft.CodeAnalysis.Text.TextSpan? filterSpan, System.Collections.Immutable.ImmutableArray<Microsoft.CodeAnalysis.Diagnostics.DiagnosticAnalyzer!> analyzers, System.Threading.CancellationToken cancellationToken) -> System.Threading.Tasks.Task<Microsoft.CodeAnalysis.Diagnostics.AnalysisResult!>!
Microsoft.CodeAnalysis.Diagnostics.CompilationWithAnalyzers.GetAnalysisResultAsync(Microsoft.CodeAnalysis.SyntaxTree! tree, Microsoft.CodeAnalysis.Text.TextSpan? filterSpan, System.Threading.CancellationToken cancellationToken) -> System.Threading.Tasks.Task<Microsoft.CodeAnalysis.Diagnostics.AnalysisResult!>!
Microsoft.CodeAnalysis.Diagnostics.CompilationWithAnalyzers.GetAnalyzerSyntaxDiagnosticsAsync(Microsoft.CodeAnalysis.SyntaxTree! tree, Microsoft.CodeAnalysis.Text.TextSpan? filterSpan, System.Collections.Immutable.ImmutableArray<Microsoft.CodeAnalysis.Diagnostics.DiagnosticAnalyzer!> analyzers, System.Threading.CancellationToken cancellationToken) -> System.Threading.Tasks.Task<System.Collections.Immutable.ImmutableArray<Microsoft.CodeAnalysis.Diagnostic!>>!
Expand Down

0 comments on commit e6655da

Please sign in to comment.