1 | using System.Collections.Immutable;
|
---|
2 | using System.Composition;
|
---|
3 | using System.Linq;
|
---|
4 | using System.Threading;
|
---|
5 | using System.Threading.Tasks;
|
---|
6 | using Microsoft.CodeAnalysis;
|
---|
7 | using Microsoft.CodeAnalysis.CodeActions;
|
---|
8 | using Microsoft.CodeAnalysis.CodeFixes;
|
---|
9 | using Microsoft.CodeAnalysis.CSharp;
|
---|
10 | using Microsoft.CodeAnalysis.CSharp.Syntax;
|
---|
11 |
|
---|
12 | namespace PersistenceCodeFix {
|
---|
13 | [ExportCodeFixProvider(LanguageNames.CSharp, Name = nameof(ObsoleteStorableConstructorFix)), Shared]
|
---|
14 | public sealed class ObsoleteStorableConstructorFix : CodeFixProvider, IDocumentCodeFixProvider {
|
---|
15 | private const string title = "Change StorableConstructor signature";
|
---|
16 |
|
---|
17 | public sealed override ImmutableArray<string> FixableDiagnosticIds {
|
---|
18 | get { return ImmutableArray.Create(ObsoleteStorableConstructorAnalyzer.DiagnosticId); }
|
---|
19 | }
|
---|
20 |
|
---|
21 | public sealed override FixAllProvider GetFixAllProvider() {
|
---|
22 | return SequentialFixAllProvider.Instance;
|
---|
23 | }
|
---|
24 |
|
---|
25 | public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context) {
|
---|
26 | var root = await context.Document.GetSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false);
|
---|
27 |
|
---|
28 | var diagnostic = context.Diagnostics.First();
|
---|
29 | var diagnosticSpan = diagnostic.Location.SourceSpan;
|
---|
30 | var ctorSyntax = root.FindToken(diagnosticSpan.Start).Parent.AncestorsAndSelf().OfType<ConstructorDeclarationSyntax>().First();
|
---|
31 |
|
---|
32 | context.RegisterCodeFix(
|
---|
33 | CodeAction.Create(
|
---|
34 | title: title,
|
---|
35 | createChangedDocument: c => FixStorableConstructorSignature(context.Document, ctorSyntax, c),
|
---|
36 | equivalenceKey: title),
|
---|
37 | diagnostic);
|
---|
38 | }
|
---|
39 |
|
---|
40 | private static async Task<Document> FixStorableConstructorSignature(Document document, ConstructorDeclarationSyntax ctorSyntax, CancellationToken cancellationToken) {
|
---|
41 | // create ctor parameters
|
---|
42 | var paramList = SyntaxFactory.ParameterList();
|
---|
43 | paramList = paramList.AddParameters(
|
---|
44 | SyntaxFactory.Parameter(
|
---|
45 | SyntaxFactory.Identifier("_"))
|
---|
46 | .WithType(SyntaxFactory.ParseTypeName("StorableConstructorFlag")));
|
---|
47 |
|
---|
48 | // create new ctor
|
---|
49 | var storableCtor = ctorSyntax.WithParameterList(paramList);
|
---|
50 |
|
---|
51 | if (ctorSyntax.Initializer != null) {
|
---|
52 | // create initializer args
|
---|
53 | var initArgs = SyntaxFactory.ArgumentList();
|
---|
54 | initArgs = initArgs.AddArguments(
|
---|
55 | SyntaxFactory.Argument(
|
---|
56 | SyntaxFactory.IdentifierName("_")));
|
---|
57 |
|
---|
58 | // create initializer
|
---|
59 | var initializer = SyntaxFactory.ConstructorInitializer(SyntaxKind.BaseConstructorInitializer, initArgs);
|
---|
60 |
|
---|
61 | storableCtor = storableCtor.WithInitializer(initializer);
|
---|
62 | }
|
---|
63 |
|
---|
64 | var root = await document.GetSyntaxRootAsync(cancellationToken) as CompilationUnitSyntax;
|
---|
65 | var newRoot = root.ReplaceNode(ctorSyntax, storableCtor);
|
---|
66 |
|
---|
67 | return document.WithSyntaxRoot(newRoot);
|
---|
68 | }
|
---|
69 |
|
---|
70 | public Task<Document> FixDocumentAsync(Document document, SyntaxNode node, CancellationToken cancellationToken) {
|
---|
71 | return FixStorableConstructorSignature(document, (ConstructorDeclarationSyntax)node, cancellationToken);
|
---|
72 | }
|
---|
73 | }
|
---|
74 | } |
---|