1
0
mirror of https://github.com/chylex/.NET-Community-Toolkit.git synced 2025-04-10 11:15:45 +02:00

Added XML docs copying from fields to generated properties

This commit is contained in:
Sergio Pedri 2021-03-29 13:28:11 +02:00
parent 341e394e9b
commit b744017678
3 changed files with 13 additions and 3 deletions
Microsoft.Toolkit.Mvvm.SourceGenerators/ComponentModel
UnitTests/UnitTests.NetCore/Mvvm

View File

@ -34,6 +34,8 @@ public void OnVisitSyntaxNode(GeneratorSyntaxContext context)
if (context.Node is FieldDeclarationSyntax { AttributeLists: { Count: > 0 } } fieldDeclaration &&
context.SemanticModel.Compilation.GetTypeByMetadataName("Microsoft.Toolkit.Mvvm.ComponentModel.ObservablePropertyAttribute") is INamedTypeSymbol attributeSymbol)
{
SyntaxTriviaList leadingTrivia = fieldDeclaration.GetLeadingTrivia();
foreach (VariableDeclaratorSyntax variableDeclarator in fieldDeclaration.Declaration.Variables)
{
if (context.SemanticModel.GetDeclaredSymbol(variableDeclarator) is IFieldSymbol fieldSymbol &&
@ -41,7 +43,7 @@ public void OnVisitSyntaxNode(GeneratorSyntaxContext context)
attributeData.ApplicationSyntaxReference is SyntaxReference syntaxReference &&
syntaxReference.GetSyntax() is AttributeSyntax attributeSyntax)
{
this.gatheredInfo.Add(new Item(variableDeclarator, fieldSymbol, attributeSyntax, attributeData));
this.gatheredInfo.Add(new Item(leadingTrivia, variableDeclarator, fieldSymbol, attributeSyntax, attributeData));
}
}
}
@ -50,11 +52,13 @@ attributeData.ApplicationSyntaxReference is SyntaxReference syntaxReference &&
/// <summary>
/// A model for a group of item representing a discovered type to process.
/// </summary>
/// <param name="LeadingTrivia">The leading trivia for the field declaration.</param>
/// <param name="FieldDeclarator">The <see cref="VariableDeclaratorSyntax"/> instance for the target field variable declaration.</param>
/// <param name="FieldSymbol">The <see cref="IFieldSymbol"/> instance for <paramref name="FieldDeclarator"/>.</param>
/// <param name="AttributeSyntax">The <see cref="AttributeSyntax"/> instance for the target attribute over <paramref name="FieldDeclarator"/>.</param>
/// <param name="AttributeData">The <see cref="AttributeData"/> instance for <paramref name="AttributeSyntax"/>.</param>
public sealed record Item(
SyntaxTriviaList LeadingTrivia,
VariableDeclaratorSyntax FieldDeclarator,
IFieldSymbol FieldSymbol,
AttributeSyntax AttributeSyntax,

View File

@ -71,7 +71,7 @@ private static void OnExecute(
var classDeclarationSyntax =
ClassDeclaration(classDeclarationSymbol.Name)
.WithModifiers(classDeclaration.Modifiers)
.AddMembers(items.Select(static item => CreatePropertyDeclaration(item.FieldSymbol)).ToArray());
.AddMembers(items.Select(static item => CreatePropertyDeclaration(item.LeadingTrivia, item.FieldSymbol)).ToArray());
TypeDeclarationSyntax typeDeclarationSyntax = classDeclarationSyntax;
@ -112,10 +112,11 @@ private static void OnExecute(
/// <summary>
/// Creates a <see cref="PropertyDeclarationSyntax"/> instance for a specified field.
/// </summary>
/// <param name="leadingTrivia">The leading trivia for the field to process.</param>
/// <param name="fieldSymbol">The input <see cref="IFieldSymbol"/> instance to process.</param>
/// <returns>A generated <see cref="PropertyDeclarationSyntax"/> instance for the input field.</returns>
[Pure]
private static PropertyDeclarationSyntax CreatePropertyDeclaration(IFieldSymbol fieldSymbol)
private static PropertyDeclarationSyntax CreatePropertyDeclaration(SyntaxTriviaList leadingTrivia, IFieldSymbol fieldSymbol)
{
// Get the field type and the target property name
string
@ -152,6 +153,7 @@ private static PropertyDeclarationSyntax CreatePropertyDeclaration(IFieldSymbol
// Construct the generated property as follows:
//
// <FIELD_TRIVIA>
// [DebuggerNonUserCode]
// [ExcludeFromCodeCoverage]
// public <FIELD_TYPE> <PROPERTY_NAME>
@ -172,6 +174,7 @@ private static PropertyDeclarationSyntax CreatePropertyDeclaration(IFieldSymbol
.AddAttributeLists(
AttributeList(SingletonSeparatedList(Attribute(IdentifierName("DebuggerNonUserCode")))),
AttributeList(SingletonSeparatedList(Attribute(IdentifierName("ExcludeFromCodeCoverage")))))
.WithLeadingTrivia(leadingTrivia)
.AddModifiers(Token(SyntaxKind.PublicKeyword))
.AddAccessorListAccessors(
AccessorDeclaration(SyntaxKind.GetAccessorDeclaration)

View File

@ -52,6 +52,9 @@ public void Test_ObservablePropertyAttribute_Events()
public partial class SampleModel : ObservableObject
{
/// <summary>
/// This is a sample data field within <see cref="SampleModel"/> of type <see cref="int"/>.
/// </summary>
[ObservableProperty]
private int data;
}