Skip to content

Commit 28b694b

Browse files
committed
Extract out suppression from the main options to simplify
1 parent f3cf646 commit 28b694b

4 files changed

Lines changed: 37 additions & 36 deletions

File tree

src/RazorSdk/SourceGenerators/IncrementalValueProviderExtensions.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,17 @@ namespace Microsoft.NET.Sdk.Razor.SourceGenerators
1414
/// <remarks>
1515
/// This should not be used outside of <see cref="IncrementalValuesProviderExtensions.AsCachedIfSuppressed{T}(IncrementalValueProvider{T}, IncrementalValueProvider{RazorSourceGenerationOptions})"/>
1616
/// </remarks>
17-
internal sealed class RazorSourceGeneratorComparer<T> : IEqualityComparer<(T Left, RazorSourceGenerationOptions Right)> where T : notnull
17+
internal sealed class RazorSourceGeneratorComparer<T> : IEqualityComparer<(T Left, bool IsSuppressed)> where T : notnull
1818
{
1919
private readonly Func<T, T, bool> _equals;
2020
public RazorSourceGeneratorComparer(Func<T, T, bool>? equals = null)
2121
{
2222
_equals = equals ?? EqualityComparer<T>.Default.Equals;
2323
}
2424

25-
public bool Equals((T Left, RazorSourceGenerationOptions Right) x, (T Left, RazorSourceGenerationOptions Right) y)
25+
public bool Equals((T Left, bool IsSuppressed) x, (T Left, bool IsSuppressed) y)
2626
{
27-
if (y.Right.SuppressRazorSourceGenerator)
27+
if (y.IsSuppressed)
2828
{
2929
// If source generation is suppressed, we can always use previously cached results.
3030
return true;
@@ -33,27 +33,27 @@ public bool Equals((T Left, RazorSourceGenerationOptions Right) x, (T Left, Razo
3333
return _equals(x.Left, y.Left);
3434
}
3535

36-
public int GetHashCode((T Left, RazorSourceGenerationOptions Right) obj) => throw new NotImplementedException("GetHashCode is never expected to be called");
36+
public int GetHashCode((T Left, bool IsSuppressed) obj) => throw new NotImplementedException("GetHashCode is never expected to be called");
3737
}
3838

3939
internal static class IncrementalValuesProviderExtensions
4040
{
4141
/// <summary>
4242
/// Adds a comparer that will force the provider to be considered as cached if the razor options call for supression
4343
/// </summary>
44-
internal static IncrementalValueProvider<T> AsCachedIfSuppressed<T>(this IncrementalValueProvider<T> provider, IncrementalValueProvider<RazorSourceGenerationOptions> options)
44+
internal static IncrementalValueProvider<T> AsCachedIfSuppressed<T>(this IncrementalValueProvider<T> provider, IncrementalValueProvider<bool> isSuppressedProvider)
4545
where T : notnull
4646
{
47-
return provider.Combine(options).WithComparer(new RazorSourceGeneratorComparer<T>()).Select((pair, _) => pair.Left);
47+
return provider.Combine(isSuppressedProvider).WithComparer(new RazorSourceGeneratorComparer<T>()).Select((pair, _) => pair.Left);
4848
}
4949

5050
/// <summary>
5151
/// Adds a comparer that will force the provider to be considered as cached if the razor options call for supression
5252
/// </summary>
53-
internal static IncrementalValuesProvider<T> AsCachedIfSuppressed<T>(this IncrementalValuesProvider<T> provider, IncrementalValueProvider<RazorSourceGenerationOptions> options)
53+
internal static IncrementalValuesProvider<T> AsCachedIfSuppressed<T>(this IncrementalValuesProvider<T> provider, IncrementalValueProvider<bool> isSuppressedProvider)
5454
where T : notnull
5555
{
56-
return provider.Combine(options).WithComparer(new RazorSourceGeneratorComparer<T>()).Select((pair, _) => pair.Left);
56+
return provider.Combine(isSuppressedProvider).WithComparer(new RazorSourceGeneratorComparer<T>()).Select((pair, _) => pair.Left);
5757
}
5858

5959

src/RazorSdk/SourceGenerators/RazorSourceGenerationOptions.cs

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,6 @@ internal sealed class RazorSourceGenerationOptions : IEquatable<RazorSourceGener
1919
/// </summary>
2020
public bool GenerateMetadataSourceChecksumAttributes { get; set; } = false;
2121

22-
/// <summary>
23-
/// Gets a flag that determines if the source generator should no-op.
24-
/// <para>
25-
/// This flag exists to support scenarios in VS where design-time and EnC builds need
26-
/// to run without invoking the source generator to avoid duplicate types being produced.
27-
/// The property is set by the SDK via an editor config.
28-
/// </para>
29-
/// </summary>
3022
public bool SuppressRazorSourceGenerator { get; set; } = false;
3123

3224
/// <summary>
@@ -40,9 +32,6 @@ internal sealed class RazorSourceGenerationOptions : IEquatable<RazorSourceGener
4032
public bool SupportLocalizedComponentNames { get; set; } = false;
4133

4234
public bool Equals(RazorSourceGenerationOptions other)
43-
=> SuppressRazorSourceGenerator == other.SuppressRazorSourceGenerator && EqualsIgnoringSupression(other);
44-
45-
public bool EqualsIgnoringSupression(RazorSourceGenerationOptions other)
4635
{
4736
return
4837
RootNamespace == other.RootNamespace &&

src/RazorSdk/SourceGenerators/RazorSourceGenerator.RazorProviders.cs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,20 @@ namespace Microsoft.NET.Sdk.Razor.SourceGenerators
1414
{
1515
public partial class RazorSourceGenerator
1616
{
17+
/// <summary>
18+
/// Gets a flag that determines if the source generator should no-op.
19+
/// <para>
20+
/// This flag exists to support scenarios in VS where design-time and EnC builds need
21+
/// to run without invoking the source generator to avoid duplicate types being produced.
22+
/// The property is set by the SDK via an editor config.
23+
/// </para>
24+
/// </summary>
25+
private static bool GetSupressionStatus(AnalyzerConfigOptionsProvider optionsProvider, CancellationToken _)
26+
{
27+
return optionsProvider.GlobalOptions.TryGetValue("build_property.SuppressRazorSourceGenerator", out var suppressRazorSourceGenerator)
28+
&& suppressRazorSourceGenerator == "true";
29+
}
30+
1731
private static (RazorSourceGenerationOptions?, Diagnostic?) ComputeRazorSourceGeneratorOptions((AnalyzerConfigOptionsProvider, ParseOptions) pair, CancellationToken ct)
1832
{
1933
Log.ComputeRazorSourceGeneratorOptions();
@@ -24,7 +38,6 @@ private static (RazorSourceGenerationOptions?, Diagnostic?) ComputeRazorSourceGe
2438
globalOptions.TryGetValue("build_property.RazorConfiguration", out var configurationName);
2539
globalOptions.TryGetValue("build_property.RootNamespace", out var rootNamespace);
2640
globalOptions.TryGetValue("build_property.SupportLocalizedComponentNames", out var supportLocalizedComponentNames);
27-
globalOptions.TryGetValue("build_property.SuppressRazorSourceGenerator", out var suppressRazorSourceGenerator);
2841
globalOptions.TryGetValue("build_property.GenerateRazorMetadataSourceChecksumAttributes", out var generateMetadataSourceChecksumAttributes);
2942

3043
var razorLanguageVersion = RazorLanguageVersion.Latest;
@@ -43,7 +56,6 @@ private static (RazorSourceGenerationOptions?, Diagnostic?) ComputeRazorSourceGe
4356
var razorSourceGenerationOptions = new RazorSourceGenerationOptions()
4457
{
4558
Configuration = razorConfiguration,
46-
SuppressRazorSourceGenerator = suppressRazorSourceGenerator == "true",
4759
GenerateMetadataSourceChecksumAttributes = generateMetadataSourceChecksumAttributes == "true",
4860
RootNamespace = rootNamespace ?? "ASP",
4961
SupportLocalizedComponentNames = supportLocalizedComponentNames == "true",

src/RazorSdk/SourceGenerators/RazorSourceGenerator.cs

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,23 +19,23 @@ public partial class RazorSourceGenerator : IIncrementalGenerator
1919

2020
public void Initialize(IncrementalGeneratorInitializationContext context)
2121
{
22-
var razorSourceGeneratorOptionsWithDiagnostics = context.AnalyzerConfigOptionsProvider
23-
.Combine(context.ParseOptionsProvider)
24-
.Select(ComputeRazorSourceGeneratorOptions);
25-
var liveRazorSourceGeneratorOptions = razorSourceGeneratorOptionsWithDiagnostics.ReportDiagnostics(context);
26-
27-
var analyzerConfigOptions = context.AnalyzerConfigOptionsProvider.AsCachedIfSuppressed(liveRazorSourceGeneratorOptions);
28-
var additionalTexts = context.AdditionalTextsProvider.AsCachedIfSuppressed(liveRazorSourceGeneratorOptions);
29-
var parseOptions = context.ParseOptionsProvider.AsCachedIfSuppressed(liveRazorSourceGeneratorOptions);
30-
var compilation = context.CompilationProvider.AsCachedIfSuppressed(liveRazorSourceGeneratorOptions);
31-
var razorSourceGeneratorOptions = liveRazorSourceGeneratorOptions.WithLambdaComparer((l, r) => l.EqualsIgnoringSupression(r), l => l.GetHashCode());
32-
33-
var sourceItemsWithDiagnostics = additionalTexts
22+
// determine if we should suppress this run and set all the inputs to cached if so
23+
var isGeneratorSuppressed = context.AnalyzerConfigOptionsProvider.Select(GetSupressionStatus);
24+
25+
var analyzerConfigOptions = context.AnalyzerConfigOptionsProvider.AsCachedIfSuppressed(isGeneratorSuppressed);
26+
var additionalTexts = context.AdditionalTextsProvider.AsCachedIfSuppressed(isGeneratorSuppressed);
27+
var parseOptions = context.ParseOptionsProvider.AsCachedIfSuppressed(isGeneratorSuppressed);
28+
var compilation = context.CompilationProvider.AsCachedIfSuppressed(isGeneratorSuppressed);
29+
30+
var razorSourceGeneratorOptions = analyzerConfigOptions
31+
.Combine(parseOptions)
32+
.Select(ComputeRazorSourceGeneratorOptions)
33+
.ReportDiagnostics(context);
34+
35+
var sourceItems = additionalTexts
3436
.Where(static (file) => file.Path.EndsWith(".razor", StringComparison.OrdinalIgnoreCase) || file.Path.EndsWith(".cshtml", StringComparison.OrdinalIgnoreCase))
3537
.Combine(analyzerConfigOptions)
36-
.Select(ComputeProjectItems);
37-
38-
var sourceItems = sourceItemsWithDiagnostics
38+
.Select(ComputeProjectItems)
3939
.ReportDiagnostics(context);
4040

4141
var hasRazorFiles = sourceItems.Collect()

0 commit comments

Comments
 (0)