-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #576 from CommunityToolkit/user/sergiopedri/uwp-ne…
…t9-appservices Add support for modern .NET to AppServices library
- Loading branch information
Showing
8 changed files
with
154 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
...kit.AppServices.SourceGenerators/Extensions/GeneratorAttributeSyntaxContextWithOptions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// 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.Collections.Immutable; | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.Diagnostics; | ||
|
||
namespace CommunityToolkit.AppServices.SourceGenerators.Extensions; | ||
|
||
/// <summary> | ||
/// <inheritdoc cref="GeneratorAttributeSyntaxContext" path="/summary/node()"/> | ||
/// </summary> | ||
/// <param name="syntaxContext">The original <see cref="GeneratorAttributeSyntaxContext"/> value.</param> | ||
/// <param name="globalOptions">The original <see cref="AnalyzerConfigOptions"/> value.</param> | ||
internal readonly struct GeneratorAttributeSyntaxContextWithOptions( | ||
GeneratorAttributeSyntaxContext syntaxContext, | ||
AnalyzerConfigOptions globalOptions) | ||
{ | ||
/// <inheritdoc cref="GeneratorAttributeSyntaxContext.TargetNode"/> | ||
public SyntaxNode TargetNode { get; } = syntaxContext.TargetNode; | ||
|
||
/// <inheritdoc cref="GeneratorAttributeSyntaxContext.TargetSymbol"/> | ||
public ISymbol TargetSymbol { get; } = syntaxContext.TargetSymbol; | ||
|
||
/// <inheritdoc cref="GeneratorAttributeSyntaxContext.SemanticModel"/> | ||
public SemanticModel SemanticModel { get; } = syntaxContext.SemanticModel; | ||
|
||
/// <inheritdoc cref="GeneratorAttributeSyntaxContext.Attributes"/> | ||
public ImmutableArray<AttributeData> Attributes { get; } = syntaxContext.Attributes; | ||
|
||
/// <inheritdoc cref="AnalyzerConfigOptionsProvider.GlobalOptions"/> | ||
public AnalyzerConfigOptions GlobalOptions { get; } = globalOptions; | ||
} |
27 changes: 27 additions & 0 deletions
27
...unityToolkit.AppServices.SourceGenerators/Extensions/GeneratorSyntaxContextWithOptions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
// 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 Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.Diagnostics; | ||
|
||
namespace CommunityToolkit.AppServices.SourceGenerators.Extensions; | ||
|
||
/// <summary> | ||
/// <inheritdoc cref="GeneratorSyntaxContext" path="/summary/node()"/> | ||
/// </summary> | ||
/// <param name="syntaxContext">The original <see cref="GeneratorSyntaxContext"/> value.</param> | ||
/// <param name="globalOptions">The original <see cref="AnalyzerConfigOptions"/> value.</param> | ||
internal readonly struct GeneratorSyntaxContextWithOptions( | ||
GeneratorSyntaxContext syntaxContext, | ||
AnalyzerConfigOptions globalOptions) | ||
{ | ||
/// <inheritdoc cref="GeneratorSyntaxContext.Node"/> | ||
public SyntaxNode Node { get; } = syntaxContext.Node; | ||
|
||
/// <inheritdoc cref="GeneratorSyntaxContext.SemanticModel"/> | ||
public SemanticModel SemanticModel { get; } = syntaxContext.SemanticModel; | ||
|
||
/// <inheritdoc cref="AnalyzerConfigOptionsProvider.GlobalOptions"/> | ||
public AnalyzerConfigOptions GlobalOptions { get; } = globalOptions; | ||
} |
59 changes: 59 additions & 0 deletions
59
...rvices.SourceGenerators/Extensions/IncrementalGeneratorInitializationContextExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
// 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.Threading; | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.Diagnostics; | ||
|
||
namespace CommunityToolkit.AppServices.SourceGenerators.Extensions; | ||
|
||
/// <summary> | ||
/// Extension methods for <see cref="IncrementalGeneratorInitializationContext"/>. | ||
/// </summary> | ||
internal static class IncrementalGeneratorInitializationContextExtensions | ||
{ | ||
/// <inheritdoc cref="SyntaxValueProvider.ForAttributeWithMetadataName"/> | ||
public static IncrementalValuesProvider<T> ForAttributeWithMetadataNameAndOptions<T>( | ||
this IncrementalGeneratorInitializationContext context, | ||
string fullyQualifiedMetadataName, | ||
Func<SyntaxNode, CancellationToken, bool> predicate, | ||
Func<GeneratorAttributeSyntaxContextWithOptions, CancellationToken, T> transform) | ||
{ | ||
// Invoke 'ForAttributeWithMetadataName' normally, but just return the context directly | ||
IncrementalValuesProvider<GeneratorAttributeSyntaxContext> syntaxContext = context.SyntaxProvider.ForAttributeWithMetadataName( | ||
fullyQualifiedMetadataName, | ||
predicate, | ||
static (context, token) => context); | ||
|
||
// Do the same for the analyzer config options | ||
IncrementalValueProvider<AnalyzerConfigOptions> configOptions = context.AnalyzerConfigOptionsProvider.Select(static (provider, token) => provider.GlobalOptions); | ||
|
||
// Merge the two and invoke the provided transform on these two values. Neither value | ||
// is equatable, meaning the pipeline will always re-run until this point. This is | ||
// intentional: we don't want any symbols or other expensive objects to be kept alive | ||
// across incremental steps, especially if they could cause entire compilations to be | ||
// rooted, which would significantly increase memory use and introduce more GC pauses. | ||
// In this specific case, flowing non equatable values in a pipeline is therefore fine. | ||
return syntaxContext.Combine(configOptions).Select((input, token) => transform(new GeneratorAttributeSyntaxContextWithOptions(input.Left, input.Right), token)); | ||
} | ||
|
||
/// <inheritdoc cref="SyntaxValueProvider.CreateSyntaxProvider"/> | ||
public static IncrementalValuesProvider<T> CreateSyntaxProviderWithOptions<T>( | ||
this IncrementalGeneratorInitializationContext context, | ||
Func<SyntaxNode, CancellationToken, bool> predicate, | ||
Func<GeneratorSyntaxContextWithOptions, CancellationToken, T> transform) | ||
{ | ||
// Invoke 'ForAttributeWithMetadataName' normally, but just return the context directly | ||
IncrementalValuesProvider<GeneratorSyntaxContext> syntaxContext = context.SyntaxProvider.CreateSyntaxProvider( | ||
predicate, | ||
static (context, token) => context); | ||
|
||
// Do the same for the analyzer config options | ||
IncrementalValueProvider<AnalyzerConfigOptions> configOptions = context.AnalyzerConfigOptionsProvider.Select(static (provider, token) => provider.GlobalOptions); | ||
|
||
// Merge the two and invoke the provided transform, like the extension above | ||
return syntaxContext.Combine(configOptions).Select((input, token) => transform(new GeneratorSyntaxContextWithOptions(input.Left, input.Right), token)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 5 additions & 0 deletions
5
components/AppServices/src/CommunityToolkit.AppServices.targets
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters