1using System.Collections.Immutable;
2using System.Composition;
4using System.Threading.Tasks;
13 [ExportCodeFixProvider(LanguageNames.CSharp)]
18 public override ImmutableArray<string>
FixableDiagnosticIds => ImmutableArray.Create(Constants.AnalyzerIdentifiers.FindSaveAssignmentIssue);
20 public sealed
override FixAllProvider
GetFixAllProvider() => WellKnownFixAllProviders.BatchFixer;
24 var root = await context.Document.GetSyntaxRootAsync(context.CancellationToken).ConfigureAwait(
false);
26 context.CancellationToken.ThrowIfCancellationRequested();
28 var diagnostic = context.Diagnostics.First();
29 var invocationNode = root.FindNode(diagnostic.Location.SourceSpan) as InvocationExpressionSyntax;
31 var invocationIdentifier = ((invocationNode.Expression as MemberAccessExpressionSyntax)
32 .Expression as IdentifierNameSyntax).Identifier;
33 var leadingTrivia = invocationIdentifier.HasLeadingTrivia ?
34 invocationIdentifier.LeadingTrivia :
new SyntaxTriviaList();
36 var newInvocationIdentifier = invocationIdentifier.WithLeadingTrivia(
new SyntaxTriviaList());
37 var newInvocationNode = invocationNode.ReplaceToken(invocationIdentifier, newInvocationIdentifier);
39 context.CancellationToken.ThrowIfCancellationRequested();
41 var simpleAssignmentExpressionNode = SyntaxFactory.AssignmentExpression(SyntaxKind.SimpleAssignmentExpression,
42 SyntaxFactory.IdentifierName(newInvocationIdentifier), newInvocationNode)
43 .WithLeadingTrivia(leadingTrivia);
45 var newRoot = root.ReplaceNode(invocationNode, simpleAssignmentExpressionNode);
47 context.RegisterCodeFix(
49 FindSaveAssignmentIssueAnalyzerAddAssignmentCodeFixConstants.AddAssignmentDescription,
50 _ => Task.FromResult(context.Document.WithSyntaxRoot(newRoot)),
51 FindSaveAssignmentIssueAnalyzerAddAssignmentCodeFixConstants.AddAssignmentDescription), diagnostic);
sealed override FixAllProvider GetFixAllProvider()
override async Task RegisterCodeFixesAsync(CodeFixContext context)
override ImmutableArray< string > FixableDiagnosticIds