diff --git a/src/ComputeSharp.CodeFixing/MissingAttributeCodeFixer.cs b/src/ComputeSharp.CodeFixing/MissingAttributeCodeFixer.cs index bc33a9b2..ade028d3 100644 --- a/src/ComputeSharp.CodeFixing/MissingAttributeCodeFixer.cs +++ b/src/ComputeSharp.CodeFixing/MissingAttributeCodeFixer.cs @@ -54,16 +54,16 @@ public abstract class MissingAttributeCodeFixer : CodeFixProvider } /// - public override ImmutableArray FixableDiagnosticIds { get; } + public sealed override ImmutableArray FixableDiagnosticIds { get; } /// - public override Microsoft.CodeAnalysis.CodeFixes.FixAllProvider? GetFixAllProvider() + public sealed override Microsoft.CodeAnalysis.CodeFixes.FixAllProvider? GetFixAllProvider() { return new FixAllProvider(this); } /// - public override async Task RegisterCodeFixesAsync(CodeFixContext context) + public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context) { Diagnostic diagnostic = context.Diagnostics[0]; TextSpan diagnosticSpan = context.Span; diff --git a/src/ComputeSharp.D2D1.CodeFixers/MissingPixelShaderDescriptorOnPixelShaderCodeFixer.cs b/src/ComputeSharp.D2D1.CodeFixers/MissingPixelShaderDescriptorOnPixelShaderCodeFixer.cs index bddd91ad..69b0a7dc 100644 --- a/src/ComputeSharp.D2D1.CodeFixers/MissingPixelShaderDescriptorOnPixelShaderCodeFixer.cs +++ b/src/ComputeSharp.D2D1.CodeFixers/MissingPixelShaderDescriptorOnPixelShaderCodeFixer.cs @@ -1,15 +1,7 @@ -using System.Collections.Immutable; using System.Composition; -using System.Threading; -using System.Threading.Tasks; -using ComputeSharp.SourceGeneration.Extensions; +using ComputeSharp.CodeFixing; using Microsoft.CodeAnalysis; -using Microsoft.CodeAnalysis.CodeActions; using Microsoft.CodeAnalysis.CodeFixes; -using Microsoft.CodeAnalysis.CSharp.Syntax; -using Microsoft.CodeAnalysis.Editing; -using Microsoft.CodeAnalysis.Simplification; -using Microsoft.CodeAnalysis.Text; using static ComputeSharp.SourceGeneration.Diagnostics.DiagnosticDescriptors; namespace ComputeSharp.D2D1.CodeFixers; @@ -19,12 +11,13 @@ namespace ComputeSharp.D2D1.CodeFixers; /// [ExportCodeFixProvider(LanguageNames.CSharp)] [Shared] -public sealed class MissingPixelShaderDescriptorOnPixelShaderCodeFixer : CodeFixProvider +public sealed class MissingPixelShaderDescriptorOnPixelShaderCodeFixer : MissingAttributeCodeFixer { /// /// The set of type names for all D2D attributes that can be over shader types. /// - private static readonly ImmutableArray D2DAttributeTypeNames = ImmutableArray.Create( + private static readonly string[] D2DAttributeTypeNames = new[] + { "ComputeSharp.D2D1.D2DCompileOptionsAttribute", "ComputeSharp.D2D1.D2DEffectAuthorAttribute", "ComputeSharp.D2D1.D2DEffectCategoryAttribute", @@ -38,171 +31,18 @@ public sealed class MissingPixelShaderDescriptorOnPixelShaderCodeFixer : CodeFix "ComputeSharp.D2D1.D2DOutputBufferAttribute", "ComputeSharp.D2D1.D2DPixelOptionsAttribute", "ComputeSharp.D2D1.D2DRequiresScenePositionAttribute", - "ComputeSharp.D2D1.D2DShaderProfileAttribute"); - - /// - public override ImmutableArray FixableDiagnosticIds { get; } = ImmutableArray.Create(MissingPixelShaderDescriptorOnPixelShaderTypeId); - - /// - public override Microsoft.CodeAnalysis.CodeFixes.FixAllProvider? GetFixAllProvider() - { - return new FixAllProvider(); - } - - /// - 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 => AddMissingD2DGeneratedPixelShaderDescriptorAttribute(context.Document, root, structDeclaration, token), - equivalenceKey: "Add [D2DGeneratedPixelShaderDescriptor] attribute"), - diagnostic); - } - } + "ComputeSharp.D2D1.D2DShaderProfileAttribute" + }; /// - /// Applies the code fix to add the [D2DGeneratedPixelShaderDescriptor] attribute to a target type. + /// Creates a new instance with the specified parameters. /// - /// 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 async Task AddMissingD2DGeneratedPixelShaderDescriptorAttribute( - Document document, - SyntaxNode root, - StructDeclarationSyntax structDeclaration, - CancellationToken cancellationToken) + public MissingPixelShaderDescriptorOnPixelShaderCodeFixer() + : base( + diagnosticId: MissingPixelShaderDescriptorOnPixelShaderTypeId, + codeActionTitle: "Add [D2DGeneratedPixelShaderDescriptor] attribute", + attributeFullyQualifiedMetadataName: "ComputeSharp.D2D1.D2DGeneratedPixelShaderDescriptorAttribute", + leadingAttributeFullyQualifiedMetadataNames: D2DAttributeTypeNames) { - // 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 structDeclaration; - } - - // Build the map of D2D attributes to look for - if (!semanticModel.Compilation.TryBuildNamedTypeSymbolSet(D2DAttributeTypeNames, out ImmutableHashSet? d2DAttributeTypeSymbols)) - { - 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 structDeclaration; - } - - int index = 0; - - // Find the index to use to insert the attribute. We want to make it so that if the struct declaration - // has a bunch of D2D attributes, the new one will be inserted right after that. This way the final list - // will be nicely sorted, instead of having D2D attributes interleaving other unrelated attributes, if any. - foreach (AttributeListSyntax attributeList in structDeclaration.AttributeLists) - { - // Make sure we have an attribute to check - if (attributeList.Attributes is not [AttributeSyntax attribute, ..]) - { - continue; - } - - // Resolve the symbol for the attribute (stop here if this failed for whatever reason) - if (!semanticModel.GetSymbolInfo(attribute, cancellationToken).TryGetAttributeTypeSymbol(out INamedTypeSymbol? attributeTypeSymbol)) - { - break; - } - - // If the attribute is D2D one, increment the index and continue - if (d2DAttributeTypeSymbols.Contains(attributeTypeSymbol)) - { - index++; - } - else - { - // Otherwise, stop here, we reached the end of the sequence - break; - } - } - - SyntaxGenerator syntaxGenerator = SyntaxGenerator.GetGenerator(document); - - // Create the attribute syntax for the new attribute. Also annotate it - // to automatically add using directives to the document, if needed. - // Then create the attribute syntax and insert it at the right position. - SyntaxNode attributeTypeSyntax = syntaxGenerator.TypeExpression(attributeSymbol).WithAdditionalAnnotations(Simplifier.AddImportsAnnotation); - SyntaxNode attributeSyntax = syntaxGenerator.Attribute(attributeTypeSyntax); - SyntaxNode updatedStructDeclarationSyntax = syntaxGenerator.InsertAttributes(structDeclaration, index, attributeSyntax); - - // 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()); - } } }