CSLA.NET 6.0.0
CSLA .NET is a software development framework that helps you build a reusable, maintainable object-oriented business layer for your app.
FindSaveAssignmentIssueAnalyzerAddAssignmentCodeFix.cs
Go to the documentation of this file.
1using System.Collections.Immutable;
2using System.Composition;
3using System.Linq;
4using System.Threading.Tasks;
5using Microsoft.CodeAnalysis;
6using Microsoft.CodeAnalysis.CodeFixes;
7using Microsoft.CodeAnalysis.CSharp.Syntax;
8using Microsoft.CodeAnalysis.CSharp;
9using Microsoft.CodeAnalysis.CodeActions;
10
11namespace Csla.Analyzers
12{
13 [ExportCodeFixProvider(LanguageNames.CSharp)]
14 [Shared]
16 : CodeFixProvider
17 {
18 public override ImmutableArray<string> FixableDiagnosticIds => ImmutableArray.Create(Constants.AnalyzerIdentifiers.FindSaveAssignmentIssue);
19
20 public sealed override FixAllProvider GetFixAllProvider() => WellKnownFixAllProviders.BatchFixer;
21
22 public override async Task RegisterCodeFixesAsync(CodeFixContext context)
23 {
24 var root = await context.Document.GetSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false);
25
26 context.CancellationToken.ThrowIfCancellationRequested();
27
28 var diagnostic = context.Diagnostics.First();
29 var invocationNode = root.FindNode(diagnostic.Location.SourceSpan) as InvocationExpressionSyntax;
30
31 var invocationIdentifier = ((invocationNode.Expression as MemberAccessExpressionSyntax)
32 .Expression as IdentifierNameSyntax).Identifier;
33 var leadingTrivia = invocationIdentifier.HasLeadingTrivia ?
34 invocationIdentifier.LeadingTrivia : new SyntaxTriviaList();
35
36 var newInvocationIdentifier = invocationIdentifier.WithLeadingTrivia(new SyntaxTriviaList());
37 var newInvocationNode = invocationNode.ReplaceToken(invocationIdentifier, newInvocationIdentifier);
38
39 context.CancellationToken.ThrowIfCancellationRequested();
40
41 var simpleAssignmentExpressionNode = SyntaxFactory.AssignmentExpression(SyntaxKind.SimpleAssignmentExpression,
42 SyntaxFactory.IdentifierName(newInvocationIdentifier), newInvocationNode)
43 .WithLeadingTrivia(leadingTrivia);
44
45 var newRoot = root.ReplaceNode(invocationNode, simpleAssignmentExpressionNode);
46
47 context.RegisterCodeFix(
48 CodeAction.Create(
49 FindSaveAssignmentIssueAnalyzerAddAssignmentCodeFixConstants.AddAssignmentDescription,
50 _ => Task.FromResult(context.Document.WithSyntaxRoot(newRoot)),
51 FindSaveAssignmentIssueAnalyzerAddAssignmentCodeFixConstants.AddAssignmentDescription), diagnostic);
52 }
53 }
54}