diff --git a/src/ComputeSharp.D2D1.UI.CodeFixers/UseGeneratedCanvasEffectPropertyOnSemiAutoPropertyCodeFixer.cs b/src/ComputeSharp.D2D1.UI.CodeFixers/UseGeneratedCanvasEffectPropertyOnSemiAutoPropertyCodeFixer.cs index 2f95c021..54b72837 100644 --- a/src/ComputeSharp.D2D1.UI.CodeFixers/UseGeneratedCanvasEffectPropertyOnSemiAutoPropertyCodeFixer.cs +++ b/src/ComputeSharp.D2D1.UI.CodeFixers/UseGeneratedCanvasEffectPropertyOnSemiAutoPropertyCodeFixer.cs @@ -116,6 +116,57 @@ public sealed class UseGeneratedCanvasEffectPropertyOnSemiAutoPropertyCodeFixer return true; } + /// + /// Updates an for the [GeneratedCanvasEffectProperty] attribute with the right default value. + /// + /// The original document being fixed. + /// The instance for the current compilation. + /// The with the attribute to add. + /// The invalidation mode to use. + /// The updated attribute syntax. + private static AttributeListSyntax UpdateGeneratedCanvasEffectPropertyAttributeList( + Document document, + SemanticModel semanticModel, + AttributeListSyntax generatedCanvasEffectPropertyAttributeList, + int invalidationMode) + { + // If the invalidation mode is not the default, set it in the attribute. + // We extract the generated attribute so we can add the new argument. + // It's important to reuse it, as it has the "add usings" annotation. + if (invalidationMode == 1) + { + // Try to resolve the attribute type, if present (this should always be the case) + if (semanticModel.Compilation.GetTypeByMetadataName(WellKnownTypeNames.CanvasEffectInvalidationType) is INamedTypeSymbol enumTypeSymbol) + { + SyntaxGenerator syntaxGenerator = SyntaxGenerator.GetGenerator(document); + + // Create the identifier syntax for the enum type, with the right annotations + SyntaxNode enumTypeSyntax = syntaxGenerator.TypeExpression(enumTypeSymbol).WithAdditionalAnnotations(Simplifier.AddImportsAnnotation); + + // Create the member access expression for the target enum type. + // We only ever take this path for the 'Creation' invalidation mode. + SyntaxNode enumMemberAccessExpressionSyntax = syntaxGenerator.MemberAccessExpression(enumTypeSyntax, "Creation"); + + // Create the attribute argument to insert + SyntaxNode attributeArgumentSyntax = syntaxGenerator.AttributeArgument("InvalidationMode", enumMemberAccessExpressionSyntax); + + // Actually add the argument to the existing attribute syntax + return (AttributeListSyntax)syntaxGenerator.AddAttributeArguments(generatedCanvasEffectPropertyAttributeList, [attributeArgumentSyntax]); + } + + // This failed... For some reason. Use the fully qualified type name as a last resort. + return + AttributeList(SingletonSeparatedList( + generatedCanvasEffectPropertyAttributeList.Attributes[0] + .AddArgumentListArguments( + AttributeArgument(ParseExpression($"global::{WellKnownTypeNames.CanvasEffectInvalidationType}.Creation")) + .WithNameEquals(NameEquals(IdentifierName("InvalidationMode")))))); + } + + // If we have no custom invalidation mode, we can just reuse the attribute with no changes + return generatedCanvasEffectPropertyAttributeList; + } + /// /// Applies the code fix to a target identifier and returns an updated document. /// @@ -144,6 +195,8 @@ public sealed class UseGeneratedCanvasEffectPropertyOnSemiAutoPropertyCodeFixer SyntaxEditor syntaxEditor = new(root, document.Project.Solution.Workspace.Services); ConvertToPartialProperty( + document, + semanticModel, propertyDeclaration, generatedCanvasEffectPropertyAttributeList, syntaxEditor, @@ -156,29 +209,27 @@ public sealed class UseGeneratedCanvasEffectPropertyOnSemiAutoPropertyCodeFixer /// /// Applies the code fix to a target identifier and returns an updated document. /// + /// The original document being fixed. + /// The instance for the current compilation. /// The for the property being updated. /// The with the attribute to add. /// The instance to use. /// The invalidation mode to use. /// An updated document with the applied code fix, and being replaced with a partial property. private static void ConvertToPartialProperty( + Document document, + SemanticModel semanticModel, PropertyDeclarationSyntax propertyDeclaration, AttributeListSyntax generatedCanvasEffectPropertyAttributeList, SyntaxEditor syntaxEditor, int invalidationMode) { - // If the invalidation mode is not the default, set it in the attribute. - // We extract the generated attribute so we can add the new argument. - // It's important to reuse it, as it has the "add usings" annotation. - if (invalidationMode == 1) - { - generatedCanvasEffectPropertyAttributeList = - AttributeList(SingletonSeparatedList( - generatedCanvasEffectPropertyAttributeList.Attributes[0] - .AddArgumentListArguments( - AttributeArgument(ParseExpression($"global::{WellKnownTypeNames.CanvasEffectInvalidationType}.Update")) - .WithNameEquals(NameEquals(IdentifierName("InvalidationMode")))))); - } + // Update the attribute to insert with the invalidation mode, if needed + generatedCanvasEffectPropertyAttributeList = UpdateGeneratedCanvasEffectPropertyAttributeList( + document, + semanticModel, + generatedCanvasEffectPropertyAttributeList, + invalidationMode); // Start setting up the updated attribute lists SyntaxList attributeLists = propertyDeclaration.AttributeLists; @@ -285,6 +336,8 @@ public sealed class UseGeneratedCanvasEffectPropertyOnSemiAutoPropertyCodeFixer } ConvertToPartialProperty( + document, + semanticModel, propertyDeclaration, generatedCanvasEffectPropertyAttributeList, syntaxEditor,