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

Add AdditionalTextValueProvider<TValue> #68092

Merged
merged 1 commit into from
May 9, 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
@@ -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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AnalysisContext.TryGetValue(Microsoft.CodeAnalysis.AdditionalText! text, Microsoft.CodeAnalysis.Diagnostics.AdditionalTextValueProvider! valueProvider, out TValue value)

It looks like this method was not included in the API review. Was that just an oversight?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. The clear intent (and approval from api review) was simply that thsi mirror the existing SyntaxTree/SourceText providers, which this falls under.

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