Add FixAllProvider and unit tests with multiple diagnostics

このコミットが含まれているのは:
Sergio Pedri committed 2023-10-03 19:22:22 +02:00
1 親 610fc5808e
コミット 1013cfb6f3
2ファイル変更
+192 -9

検索条件に一致するファイルはありません

+73 -9
ファイルの表示
@@ -24,9 +24,9 @@ public sealed class MissingPixelShaderDescriptorOnPixelShaderCodeFixer : CodeFix
public override ImmutableArray<string> FixableDiagnosticIds { get; } = ImmutableArray.Create(MissingPixelShaderDescriptorOnPixelShaderTypeId);
/// <inheritdoc/>
public override FixAllProvider? GetFixAllProvider()
public override Microsoft.CodeAnalysis.CodeFixes.FixAllProvider? GetFixAllProvider()
{
return WellKnownFixAllProviders.BatchFixer;
return new FixAllProvider();
}
/// <inheritdoc/>
@@ -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
/// <param name="structDeclaration">The <see cref="StructDeclarationSyntax"/> to update.</param>
/// <param name="cancellationToken">The cancellation token for the operation.</param>
/// <returns>An updated document with the applied code fix, and the return type of the method being <see cref="Task"/>.</returns>
private static async Task<Document> ChangeReturnType(Document document, SyntaxNode root, StructDeclarationSyntax structDeclaration, CancellationToken cancellationToken)
private static async Task<Document> 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));
}
/// <summary>
/// Applies the code fix to add the [D2DGeneratedPixelShaderDescriptor] attribute to a target type.
/// </summary>
/// <param name="document">The original document being fixed.</param>
/// <param name="structDeclaration">The <see cref="StructDeclarationSyntax"/> to update.</param>
/// <param name="cancellationToken">The cancellation token for the operation.</param>
/// <returns>An updated document with the applied code fix, and the return type of the method being <see cref="Task"/>.</returns>
private static async Task<SyntaxNode> 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;
}
/// <summary>
/// A custom <see cref="FixAllProvider"/> with the logic from <see cref="MissingPixelShaderDescriptorOnPixelShaderCodeFixer"/>.
/// </summary>
private sealed class FixAllProvider : DocumentBasedFixAllProvider
{
/// <inheritdoc/>
protected override async Task<Document?> FixAllAsync(FixAllContext fixAllContext, Document document, ImmutableArray<Diagnostic> 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());
}
}
}
@@ -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()
{