Complete prototype to also generate code
このコミットが含まれているのは:
1 親
58ad9e8bc9
コミット
44959ce417
3ファイル変更
+125
-2
検索条件に一致するファイルはありません
@@ -1,5 +1,7 @@
|
||||
using System.Threading;
|
||||
using ComputeSharp.D2D1.WinUI.SourceGenerators.Models;
|
||||
using ComputeSharp.SourceGeneration.Extensions;
|
||||
using ComputeSharp.SourceGeneration.Helpers;
|
||||
using Microsoft.CodeAnalysis;
|
||||
using Microsoft.CodeAnalysis.CSharp;
|
||||
using Microsoft.CodeAnalysis.CSharp.Syntax;
|
||||
@@ -69,5 +71,98 @@ partial class CanvasEffectPropertyGenerator
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public static void WritePropertyDeclarations(EquatableArray<CanvasEffectPropertyInfo> properties, IndentedTextWriter writer)
|
||||
{
|
||||
// First, generate all partial property implementations at the top of the partial type declaration
|
||||
foreach (CanvasEffectPropertyInfo propertyInfo in properties)
|
||||
{
|
||||
writer.WriteLine(skipIfPresent: true);
|
||||
writer.WriteLine("/// <inheritdoc/>");
|
||||
writer.WriteGeneratedAttributes(GeneratorName);
|
||||
writer.WriteLine($$"""
|
||||
public partial {{propertyInfo.TypeNameWithNullabilityAnnotations}} {{propertyInfo.PropertyName}}
|
||||
{
|
||||
get => field;
|
||||
set
|
||||
{
|
||||
if (global::System.Collections.Generic.EqualityComparer<{{propertyInfo.TypeNameWithNullabilityAnnotations}}>.Default.Equals(field, value))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
int oldValue = field;
|
||||
|
||||
On{{propertyInfo.PropertyName}}Changing(value);
|
||||
On{{propertyInfo.PropertyName}}Changing(oldValue, value);
|
||||
|
||||
field = value;
|
||||
|
||||
On{{propertyInfo.PropertyName}}Changed(value);
|
||||
On{{propertyInfo.PropertyName}}Changed(oldValue, value);
|
||||
|
||||
InvalidateEffectGraph(default);
|
||||
}
|
||||
}
|
||||
""", isMultiline: true);
|
||||
}
|
||||
|
||||
foreach (CanvasEffectPropertyInfo propertyInfo in properties)
|
||||
{
|
||||
// On<PROPERTY_NAME>Changing, only with new value
|
||||
writer.WriteLine();
|
||||
writer.WriteLine($"""
|
||||
/// <summary>Executes the logic for when <see cref="{propertyInfo.PropertyName}"/> is changing.</summary>
|
||||
/// <param name="value">The new property value being set.</param>
|
||||
/// <remarks>This method is invoked right before the value of <see cref="{propertyInfo.PropertyName}"/> is changed.</remarks>
|
||||
""", isMultiline: true);
|
||||
writer.WriteGeneratedAttributes(GeneratorName, includeNonUserCodeAttributes: false);
|
||||
writer.WriteLine($"partial void On{propertyInfo.PropertyName}Changing({propertyInfo.TypeNameWithNullabilityAnnotations} newValue);");
|
||||
|
||||
// Prepare the nullable type for the previous property value. This is needed because if the type is a reference
|
||||
// type, the previous value might be null even if the property type is not nullable, as the first invocation would
|
||||
// happen when the property is first set to some value that is not null (but the backing field would still be so).
|
||||
// As a cheap way to check whether we need to add nullable, we can simply check whether the type name with nullability
|
||||
// annotations ends with a '?'. If it doesn't and the type is a reference type, we add it. Otherwise, we keep it.
|
||||
string oldValueTypeNameAsNullable = propertyInfo.IsReferenceTypeOrUnconstraindTypeParameter switch
|
||||
{
|
||||
true when !propertyInfo.TypeNameWithNullabilityAnnotations.EndsWith("?")
|
||||
=> $"{propertyInfo.TypeNameWithNullabilityAnnotations}?",
|
||||
_ => propertyInfo.TypeNameWithNullabilityAnnotations
|
||||
};
|
||||
|
||||
// On<PROPERTY_NAME>Changing, with both values
|
||||
writer.WriteLine();
|
||||
writer.WriteLine($"""
|
||||
/// <summary>Executes the logic for when <see cref="{propertyInfo.PropertyName}"/> is changing.</summary>
|
||||
/// <param name="oldValue">The previous property value that is being replaced.</param>
|
||||
/// <param name="newValue">The new property value being set.</param>
|
||||
/// <remarks>This method is invoked right before the value of <see cref="{propertyInfo.PropertyName}"/> is changed.</remarks>
|
||||
""", isMultiline: true);
|
||||
writer.WriteGeneratedAttributes(GeneratorName, includeNonUserCodeAttributes: false);
|
||||
writer.WriteLine($"partial void On{propertyInfo.PropertyName}Changing({oldValueTypeNameAsNullable} oldValue, {propertyInfo.TypeNameWithNullabilityAnnotations} newValue);");
|
||||
|
||||
// On<PROPERTY_NAME>Changed, only with new value
|
||||
writer.WriteLine();
|
||||
writer.WriteLine($"""
|
||||
/// <summary>Executes the logic for when <see cref="{propertyInfo.PropertyName}"/> has just changed.</summary>
|
||||
/// <param name="value">The new property value that has been set.</param>
|
||||
/// <remarks>This method is invoked right after the value of <see cref="{propertyInfo.PropertyName}"/> is changed.</remarks>
|
||||
""", isMultiline: true);
|
||||
writer.WriteGeneratedAttributes(GeneratorName, includeNonUserCodeAttributes: false);
|
||||
writer.WriteLine($"partial void On{propertyInfo.PropertyName}Changed({propertyInfo.TypeNameWithNullabilityAnnotations} newValue);");
|
||||
|
||||
// On<PROPERTY_NAME>Changed, with both values
|
||||
writer.WriteLine();
|
||||
writer.WriteLine($"""
|
||||
/// <summary>Executes the logic for when <see cref="{propertyInfo.PropertyName}"/> has just changed.</summary>
|
||||
/// <param name="oldValue">The previous property value that has been replaced.</param>
|
||||
/// <param name="newValue">The new property value that has been set.</param>
|
||||
/// <remarks>This method is invoked right after the value of <see cref="{propertyInfo.PropertyName}"/> is changed.</remarks>
|
||||
""", isMultiline: true);
|
||||
writer.WriteGeneratedAttributes(GeneratorName, includeNonUserCodeAttributes: false);
|
||||
writer.WriteLine($"partial void On{propertyInfo.PropertyName}Changed({oldValueTypeNameAsNullable} oldValue, {propertyInfo.TypeNameWithNullabilityAnnotations} newValue);");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
using ComputeSharp.D2D1.WinUI.SourceGenerators.Models;
|
||||
using ComputeSharp.SourceGeneration.Extensions;
|
||||
using ComputeSharp.SourceGeneration.Helpers;
|
||||
using ComputeSharp.SourceGeneration.Models;
|
||||
using Microsoft.CodeAnalysis;
|
||||
|
||||
@@ -41,6 +42,11 @@ public sealed partial class CanvasEffectPropertyGenerator : IIncrementalGenerato
|
||||
string typeNameWithNullabilityAnnotations = propertySymbol.Type.GetFullyQualifiedNameWithNullabilityAnnotations();
|
||||
bool isOldPropertyValueDirectlyReferenced = Execute.IsOldPropertyValueDirectlyReferenced(propertySymbol);
|
||||
|
||||
// We're using IsValueType here and not IsReferenceType to also cover unconstrained type parameter cases.
|
||||
// This will cover both reference types as well T when the constraints are not struct or unmanaged.
|
||||
// If this is true, it means the field storage can potentially be in a null state (even if not annotated).
|
||||
bool isReferenceTypeOrUnconstraindTypeParameter = !propertySymbol.Type.IsValueType;
|
||||
|
||||
// Finally, get the hierarchy too
|
||||
HierarchyInfo hierarchyInfo = HierarchyInfo.From(typeSymbol);
|
||||
|
||||
@@ -50,8 +56,28 @@ public sealed partial class CanvasEffectPropertyGenerator : IIncrementalGenerato
|
||||
Hierarchy: hierarchyInfo,
|
||||
PropertyName: propertySymbol.Name,
|
||||
TypeNameWithNullabilityAnnotations: typeNameWithNullabilityAnnotations,
|
||||
IsOldPropertyValueDirectlyReferenced: isOldPropertyValueDirectlyReferenced);
|
||||
IsOldPropertyValueDirectlyReferenced: isOldPropertyValueDirectlyReferenced,
|
||||
IsReferenceTypeOrUnconstraindTypeParameter: isReferenceTypeOrUnconstraindTypeParameter);
|
||||
})
|
||||
.Where(static item => item is not null)!;
|
||||
|
||||
// Split and group by containing type
|
||||
IncrementalValuesProvider<(HierarchyInfo Hierarchy, EquatableArray<CanvasEffectPropertyInfo> Properties)> groupedPropertyInfo =
|
||||
propertyInfo
|
||||
.GroupBy(keySelector: static item => item.Hierarchy, elementSelector: static item => item);
|
||||
|
||||
// Generate the source files, if any
|
||||
context.RegisterSourceOutput(groupedPropertyInfo, static (context, item) =>
|
||||
{
|
||||
using IndentedTextWriter writer = new();
|
||||
|
||||
item.Hierarchy.WriteSyntax(
|
||||
state: item.Properties,
|
||||
writer: writer,
|
||||
baseTypes: [],
|
||||
memberCallbacks: [Execute.WritePropertyDeclarations]);
|
||||
|
||||
context.AddSource($"{item.Hierarchy.FullyQualifiedMetadataName}.g.cs", writer.ToString());
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -9,8 +9,10 @@ namespace ComputeSharp.D2D1.WinUI.SourceGenerators.Models;
|
||||
/// <param name="TypeNameWithNullabilityAnnotations">The type name for the generated property, including nullability annotations.</param>
|
||||
/// <param name="PropertyName">The generated property name.</param>
|
||||
/// <param name="IsOldPropertyValueDirectlyReferenced">Whether the old property value is being directly referenced.</param>
|
||||
/// <param name="IsReferenceTypeOrUnconstraindTypeParameter">Indicates whether the property is of a reference type or an unconstrained type parameter.</param>
|
||||
internal sealed record CanvasEffectPropertyInfo(
|
||||
HierarchyInfo Hierarchy,
|
||||
string PropertyName,
|
||||
string TypeNameWithNullabilityAnnotations,
|
||||
bool IsOldPropertyValueDirectlyReferenced);
|
||||
bool IsOldPropertyValueDirectlyReferenced,
|
||||
bool IsReferenceTypeOrUnconstraindTypeParameter);
|
||||
新しいイシューから参照
ユーザーをブロックする