diff --git a/src/ComputeSharp.D2D1.CodeFixers/MissingPixelShaderDescriptorOnPixelShaderCodeFixer.cs b/src/ComputeSharp.D2D1.CodeFixers/MissingPixelShaderDescriptorOnPixelShaderCodeFixer.cs index 05663692..9688b5b3 100644 --- a/src/ComputeSharp.D2D1.CodeFixers/MissingPixelShaderDescriptorOnPixelShaderCodeFixer.cs +++ b/src/ComputeSharp.D2D1.CodeFixers/MissingPixelShaderDescriptorOnPixelShaderCodeFixer.cs @@ -24,9 +24,9 @@ public sealed class MissingPixelShaderDescriptorOnPixelShaderCodeFixer : CodeFix public override ImmutableArray FixableDiagnosticIds { get; } = ImmutableArray.Create(MissingPixelShaderDescriptorOnPixelShaderTypeId); /// - public override FixAllProvider? GetFixAllProvider() + public override Microsoft.CodeAnalysis.CodeFixes.FixAllProvider? GetFixAllProvider() { - return WellKnownFixAllProviders.BatchFixer; + return new FixAllProvider(); } /// @@ -38,13 +38,13 @@ public sealed class MissingPixelShaderDescriptorOnPixelShaderCodeFixer : CodeFix SyntaxNode? root = await context.Document.GetSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false); // Get the struct declaration from the target diagnostic - if (root!.FindNode(diagnosticSpan) is StructDeclarationSyntax structDeclaration) + if (root?.FindNode(diagnosticSpan) is StructDeclarationSyntax structDeclaration) { // Register the code fix to update the return type to be Task instead context.RegisterCodeFix( CodeAction.Create( title: "Add [D2DGeneratedPixelShaderDescriptor] attribute", - createChangedDocument: token => ChangeReturnType(context.Document, root, structDeclaration, token), + createChangedDocument: token => AddMissingD2DGeneratedPixelShaderDescriptorAttribute(context.Document, root, structDeclaration, token), equivalenceKey: "Add [D2DGeneratedPixelShaderDescriptor] attribute"), diagnostic); } @@ -58,18 +58,44 @@ public sealed class MissingPixelShaderDescriptorOnPixelShaderCodeFixer : CodeFix /// The to update. /// The cancellation token for the operation. /// An updated document with the applied code fix, and the return type of the method being . - private static async Task ChangeReturnType(Document document, SyntaxNode root, StructDeclarationSyntax structDeclaration, CancellationToken cancellationToken) + private static async Task AddMissingD2DGeneratedPixelShaderDescriptorAttribute( + Document document, + SyntaxNode root, + StructDeclarationSyntax structDeclaration, + CancellationToken cancellationToken) + { + // Get the new struct declaration + SyntaxNode updatedStructDeclaration = await AddMissingD2DGeneratedPixelShaderDescriptorAttribute( + document, + structDeclaration, + cancellationToken); + + // Replace the node in the document tree + return document.WithSyntaxRoot(root.ReplaceNode(structDeclaration, updatedStructDeclaration)); + } + + /// + /// Applies the code fix to add the [D2DGeneratedPixelShaderDescriptor] attribute to a target type. + /// + /// The original document being fixed. + /// The to update. + /// The cancellation token for the operation. + /// An updated document with the applied code fix, and the return type of the method being . + private static async Task AddMissingD2DGeneratedPixelShaderDescriptorAttribute( + Document document, + StructDeclarationSyntax structDeclaration, + CancellationToken cancellationToken) { // Get the semantic model (bail if it's not available) if (await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false) is not SemanticModel semanticModel) { - return document; + return structDeclaration; } // Also bail if we can't resolve the [D2DGeneratedPixelShaderDescriptor] attribute symbol (this should really never happen) if (semanticModel.Compilation.GetTypeByMetadataName("ComputeSharp.D2D1.D2DGeneratedPixelShaderDescriptorAttribute") is not INamedTypeSymbol attributeSymbol) { - return document; + return structDeclaration; } int index = 0; @@ -112,7 +138,45 @@ public sealed class MissingPixelShaderDescriptorOnPixelShaderCodeFixer : CodeFix SyntaxNode attributeSyntax = syntaxGenerator.Attribute(attributeTypeSyntax); SyntaxNode updatedStructDeclarationSyntax = syntaxGenerator.InsertAttributes(structDeclaration, index, attributeSyntax); - // Replace the node in the document tree - return document.WithSyntaxRoot(root.ReplaceNode(structDeclaration, updatedStructDeclarationSyntax)); + // Replace the node in the syntax tree + return updatedStructDeclarationSyntax; + } + + /// + /// A custom with the logic from . + /// + private sealed class FixAllProvider : DocumentBasedFixAllProvider + { + /// + protected override async Task FixAllAsync(FixAllContext fixAllContext, Document document, ImmutableArray diagnostics) + { + // Get the document root (this should always succeed) + if (await document.GetSyntaxRootAsync(fixAllContext.CancellationToken).ConfigureAwait(false) is not SyntaxNode root) + { + return document; + } + + SyntaxEditor syntaxEditor = new(root, fixAllContext.Solution.Services); + + foreach (Diagnostic diagnostic in diagnostics) + { + // Get the current struct declaration for the diagnostic + if (root.FindNode(diagnostic.Location.SourceSpan) is not StructDeclarationSyntax structDeclaration) + { + continue; + } + + // Get the syntax node with the updated declaration + SyntaxNode updatedStructDeclaration = await AddMissingD2DGeneratedPixelShaderDescriptorAttribute( + document, + structDeclaration, + fixAllContext.CancellationToken); + + // Replace the node via the editor + syntaxEditor.ReplaceNode(structDeclaration, updatedStructDeclaration); + } + + return document.WithSyntaxRoot(syntaxEditor.GetChangedRoot()); + } } } diff --git a/tests/ComputeSharp.D2D1.Tests.SourceGenerators/Test_MissingPixelShaderDescriptorOnPixelShaderCodeFixer.cs b/tests/ComputeSharp.D2D1.Tests.SourceGenerators/Test_MissingPixelShaderDescriptorOnPixelShaderCodeFixer.cs index d8585425..dc404974 100644 --- a/tests/ComputeSharp.D2D1.Tests.SourceGenerators/Test_MissingPixelShaderDescriptorOnPixelShaderCodeFixer.cs +++ b/tests/ComputeSharp.D2D1.Tests.SourceGenerators/Test_MissingPixelShaderDescriptorOnPixelShaderCodeFixer.cs @@ -50,6 +50,67 @@ public class Test_MissingPixelShaderDescriptorOnPixelShaderCodeFixer await test.RunAsync(); } + [TestMethod] + public async Task FileContainsComputeSharpD2D1UsingDirective_MultipleOccurrences() + { + string original = """ + using System; + using ComputeSharp.D2D1; + + partial struct {|CMPSD2D0065:MyShader1|} : ID2D1PixelShader + { + public ComputeSharp.Float4 Execute() => 0; + } + + [D2DInputCount(0)] + [TestAttribute] + partial struct {|CMPSD2D0065:MyShader2|} : ID2D1PixelShader + { + public ComputeSharp.Float4 Execute() => 0; + } + + public class TestAttribute : Attribute + { + } + """; + + string @fixed = """ + using System; + using ComputeSharp.D2D1; + + [D2DGeneratedPixelShaderDescriptor] + partial struct MyShader1 : ID2D1PixelShader + { + public ComputeSharp.Float4 Execute() => 0; + } + + [D2DInputCount(0)] + [D2DGeneratedPixelShaderDescriptor] + [TestAttribute] + partial struct MyShader2 : ID2D1PixelShader + { + public ComputeSharp.Float4 Execute() => 0; + } + + public class TestAttribute : Attribute + { + } + """; + + CSharpCodeFixTest test = new() + { + TestCode = original, + FixedCode = @fixed, + ReferenceAssemblies = ReferenceAssemblies.Net.Net60 + }; + + test.TestState.AdditionalReferences.Add(typeof(Core::ComputeSharp.Float4).Assembly); + test.TestState.AdditionalReferences.Add(typeof(D2D1::ComputeSharp.D2D1.ID2D1PixelShader).Assembly); + test.TestState.AnalyzerConfigFiles.Add(("/.editorconfig", "[*]\nend_of_line = lf")); + + await test.RunAsync(); + } + [TestMethod] public async Task FileDoesNotContainComputeSharpD2D1UsingDirective() { @@ -84,6 +145,64 @@ public class Test_MissingPixelShaderDescriptorOnPixelShaderCodeFixer await test.RunAsync(); } + [TestMethod] + public async Task FileDoesNotContainComputeSharpD2D1UsingDirective_MultipleOccurrences() + { + string original = """ + using System; + + partial struct {|CMPSD2D0065:MyShader1|} : ComputeSharp.D2D1.ID2D1PixelShader + { + public ComputeSharp.Float4 Execute() => 0; + } + + [TestAttribute] + partial struct {|CMPSD2D0065:MyShader2|} : ComputeSharp.D2D1.ID2D1PixelShader + { + public ComputeSharp.Float4 Execute() => 0; + } + + public class TestAttribute : Attribute + { + } + """; + + string @fixed = """ + using System; + using ComputeSharp.D2D1; + + [D2DGeneratedPixelShaderDescriptor] + partial struct MyShader1 : ComputeSharp.D2D1.ID2D1PixelShader + { + public ComputeSharp.Float4 Execute() => 0; + } + + [D2DGeneratedPixelShaderDescriptor] + [TestAttribute] + partial struct MyShader2 : ComputeSharp.D2D1.ID2D1PixelShader + { + public ComputeSharp.Float4 Execute() => 0; + } + + public class TestAttribute : Attribute + { + } + """; + + CSharpCodeFixTest test = new() + { + TestCode = original, + FixedCode = @fixed, + ReferenceAssemblies = ReferenceAssemblies.Net.Net60 + }; + + test.TestState.AdditionalReferences.Add(typeof(Core::ComputeSharp.Float4).Assembly); + test.TestState.AdditionalReferences.Add(typeof(D2D1::ComputeSharp.D2D1.ID2D1PixelShader).Assembly); + test.TestState.AnalyzerConfigFiles.Add(("/.editorconfig", "[*]\nend_of_line = lf")); + + await test.RunAsync(); + } + [TestMethod] public async Task InsertsAttributeAfterAllOtherD2DAttributes() {