|
| 1 | +using System.Composition; |
| 2 | +using Microsoft.CodeAnalysis; |
| 3 | +using Microsoft.CodeAnalysis.CodeActions; |
| 4 | +using Microsoft.CodeAnalysis.CodeRefactorings; |
| 5 | + |
| 6 | +namespace EntityFrameworkCore.Projectables.CodeFixes; |
| 7 | + |
| 8 | +/// <summary> |
| 9 | +/// Code refactoring provider that converts a <c>[Projectable]</c> factory method whose body is |
| 10 | +/// an object-initializer expression (<c>=> new T { … }</c>) into a <c>[Projectable]</c> |
| 11 | +/// constructor of the same class. |
| 12 | +/// <para> |
| 13 | +/// Two refactoring actions are offered: |
| 14 | +/// <list type="number"> |
| 15 | +/// <item><description>Convert the factory method to a constructor (current document only).</description></item> |
| 16 | +/// <item><description>Convert the factory method to a constructor <em>and</em> replace all |
| 17 | +/// callers throughout the solution with <c>new T(…)</c> invocations.</description></item> |
| 18 | +/// </list> |
| 19 | +/// </para> |
| 20 | +/// <para> |
| 21 | +/// This provider is complementary to <see cref="FactoryMethodToCtorCodeFixProvider"/>, |
| 22 | +/// which fixes the <c>EFP0012</c> diagnostic. The refactoring provider remains useful when |
| 23 | +/// the diagnostic is suppressed. |
| 24 | +/// </para> |
| 25 | +/// <para> |
| 26 | +/// A public parameterless constructor is automatically inserted when the class does not already |
| 27 | +/// have one, preserving the implicit default constructor that would otherwise be lost. |
| 28 | +/// </para> |
| 29 | +/// </summary> |
| 30 | +[ExportCodeRefactoringProvider(LanguageNames.CSharp, Name = nameof(FactoryMethodToConstructorCodeRefactoringProvider))] |
| 31 | +[Shared] |
| 32 | +public sealed class FactoryMethodToConstructorCodeRefactoringProvider : CodeRefactoringProvider |
| 33 | +{ |
| 34 | + /// <inheritdoc /> |
| 35 | + public override async Task ComputeRefactoringsAsync(CodeRefactoringContext context) |
| 36 | + { |
| 37 | + var root = await context.Document.GetSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false); |
| 38 | + if (root is null) |
| 39 | + { |
| 40 | + return; |
| 41 | + } |
| 42 | + |
| 43 | + var node = root.FindNode(context.Span); |
| 44 | + |
| 45 | + if (!ProjectableCodeFixHelper.TryGetFixableFactoryMethodPattern(node, out var containingType, out var method)) |
| 46 | + { |
| 47 | + return; |
| 48 | + } |
| 49 | + |
| 50 | + context.RegisterRefactoring( |
| 51 | + CodeAction.Create( |
| 52 | + title: "Convert [Projectable] factory method to constructor", |
| 53 | + createChangedDocument: ct => |
| 54 | + FactoryMethodTransformationHelper.ConvertToConstructorAsync( |
| 55 | + context.Document, method!, containingType!, ct), |
| 56 | + equivalenceKey: "EFP_FactoryToConstructor")); |
| 57 | + |
| 58 | + context.RegisterRefactoring( |
| 59 | + CodeAction.Create( |
| 60 | + title: "Convert [Projectable] factory method to constructor (and update callers)", |
| 61 | + createChangedSolution: ct => |
| 62 | + FactoryMethodTransformationHelper.ConvertToConstructorAndUpdateCallersAsync( |
| 63 | + context.Document, method!, containingType!, ct), |
| 64 | + equivalenceKey: "EFP_FactoryToConstructorWithCallers")); |
| 65 | + } |
| 66 | +} |
0 commit comments