diff --git a/src/ComputeSharp.D2D1.CodeFixers/ComputeSharp.D2D1.CodeFixers.csproj b/src/ComputeSharp.D2D1.CodeFixers/ComputeSharp.D2D1.CodeFixers.csproj index 9f5c4f4a..41ca05c4 100644 --- a/src/ComputeSharp.D2D1.CodeFixers/ComputeSharp.D2D1.CodeFixers.csproj +++ b/src/ComputeSharp.D2D1.CodeFixers/ComputeSharp.D2D1.CodeFixers.csproj @@ -1,7 +1,13 @@ - netstandard2.0 + + + + + + + diff --git a/src/ComputeSharp.D2D1.CodeFixers/MissingPixelShaderDescriptorOnPixelShaderCodeFixer.cs b/src/ComputeSharp.D2D1.CodeFixers/MissingPixelShaderDescriptorOnPixelShaderCodeFixer.cs new file mode 100644 index 00000000..61e775e8 --- /dev/null +++ b/src/ComputeSharp.D2D1.CodeFixers/MissingPixelShaderDescriptorOnPixelShaderCodeFixer.cs @@ -0,0 +1,74 @@ +using System.Collections.Immutable; +using System.Composition; +using System.Threading; +using System.Threading.Tasks; +using Microsoft.CodeAnalysis; +using Microsoft.CodeAnalysis.CodeActions; +using Microsoft.CodeAnalysis.CodeFixes; +using Microsoft.CodeAnalysis.CSharp.Syntax; +using Microsoft.CodeAnalysis.Editing; +using Microsoft.CodeAnalysis.Text; +using static ComputeSharp.SourceGeneration.Diagnostics.DiagnosticDescriptors; +using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory; + +namespace ComputeSharp.D2D1.CodeFixers; + +/// +/// A code fixer that adds the [D2DGeneratedPixelShaderDescriptor] to D2D1 shader types with no descriptor. +/// +[ExportCodeFixProvider(LanguageNames.CSharp)] +[Shared] +public sealed class MissingPixelShaderDescriptorOnPixelShaderCodeFixer : CodeFixProvider +{ + /// + public override ImmutableArray FixableDiagnosticIds { get; } = ImmutableArray.Create(MissingPixelShaderDescriptorOnPixelShaderTypeId); + + /// + public override FixAllProvider? GetFixAllProvider() + { + return WellKnownFixAllProviders.BatchFixer; + } + + /// + public override async Task RegisterCodeFixesAsync(CodeFixContext context) + { + Diagnostic diagnostic = context.Diagnostics[0]; + TextSpan diagnosticSpan = context.Span; + + 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) + { + // 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), + equivalenceKey: "Add [D2DGeneratedPixelShaderDescriptor] attribute"), + diagnostic); + } + } + + /// + /// Applies the code fix to add the [D2DGeneratedPixelShaderDescriptor] attribute to a target type. + /// + /// The original document being fixed. + /// The original tree root belonging to the current document. + /// 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 Task ChangeReturnType(Document document, SyntaxNode root, StructDeclarationSyntax structDeclaration, CancellationToken cancellationToken) + { + // Create the attribute syntax + AttributeListSyntax attributeList = AttributeList(SingletonSeparatedList(Attribute(IdentifierName("D2DGeneratedPixelShaderDescriptor")))); + + // Create a new syntax node with the new attribute + SyntaxNode typeSyntax = SyntaxGenerator.GetGenerator(document).AddAttributes(structDeclaration, attributeList); + + // Replace the node in the document tree + Document updatedDocument = document.WithSyntaxRoot(root.ReplaceNode(structDeclaration, typeSyntax)); + + return Task.FromResult(updatedDocument); + } +} diff --git a/src/ComputeSharp.D2D1.SourceGenerators/ComputeSharp.D2D1.SourceGenerators.csproj b/src/ComputeSharp.D2D1.SourceGenerators/ComputeSharp.D2D1.SourceGenerators.csproj index 8240a9ff..934ad644 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/ComputeSharp.D2D1.SourceGenerators.csproj +++ b/src/ComputeSharp.D2D1.SourceGenerators/ComputeSharp.D2D1.SourceGenerators.csproj @@ -108,4 +108,9 @@ + + + + + diff --git a/src/Directory.Build.props b/src/Directory.Build.props index 8c96bd8c..98b02693 100644 --- a/src/Directory.Build.props +++ b/src/Directory.Build.props @@ -78,7 +78,7 @@ Reference PolySharp for all .NET Standard 2.0 polyfills. Only needed for the .Core project and all analyzers. --> - + all build;analyzers diff --git a/tests/ComputeSharp.D2D1.Tests/ComputeSharp.D2D1.Tests.csproj b/tests/ComputeSharp.D2D1.Tests/ComputeSharp.D2D1.Tests.csproj index db4c547d..a6633afc 100644 --- a/tests/ComputeSharp.D2D1.Tests/ComputeSharp.D2D1.Tests.csproj +++ b/tests/ComputeSharp.D2D1.Tests/ComputeSharp.D2D1.Tests.csproj @@ -20,6 +20,7 @@ +