|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | + |
| 4 | +using Microsoft.CodeAnalysis; |
| 5 | +using Microsoft.CodeAnalysis.CodeActions; |
| 6 | +using Microsoft.CodeAnalysis.CodeFixes; |
| 7 | +using Microsoft.CodeAnalysis.CSharp; |
| 8 | +using Microsoft.CodeAnalysis.CSharp.Syntax; |
| 9 | +using Microsoft.CodeAnalysis.Editing; |
| 10 | +using Microsoft.CodeAnalysis.Simplification; |
| 11 | +using System; |
| 12 | +using System.Collections.Immutable; |
| 13 | +using System.Linq; |
| 14 | +using System.Threading; |
| 15 | +using System.Threading.Tasks; |
| 16 | + |
| 17 | +namespace Epi.Source.Updater |
| 18 | +{ |
| 19 | + /// <summary> |
| 20 | + /// As with the analzyers, code fix providers that are registered into Upgrade Assistant's |
| 21 | + /// dependency injection container (by IExtensionServiceProvider) will be used during |
| 22 | + /// the source update step. |
| 23 | + /// </summary> |
| 24 | + [ExportCodeFixProvider(LanguageNames.CSharp, Name = "EP0006 CodeFix Provider")] |
| 25 | + public class EpiPartialControllerCodeFixProvider : CodeFixProvider |
| 26 | + { |
| 27 | + // The Upgrade Assistant will only use analyzers that have an associated code fix provider registered including |
| 28 | + // the analyzer's ID in the code fix provider's FixableDiagnosticIds array. |
| 29 | + public sealed override ImmutableArray<string> FixableDiagnosticIds => ImmutableArray.Create(EpiPartialControllerAnalyzer.DiagnosticId); |
| 30 | + private const string MvcNamespace = "Microsoft.AspNetCore.Mvc"; |
| 31 | + |
| 32 | + public sealed override FixAllProvider GetFixAllProvider() => |
| 33 | + // See https://github.com/dotnet/roslyn/blob/master/docs/analyzers/FixAllProvider.md for more information on Fix All Providers |
| 34 | + WellKnownFixAllProviders.BatchFixer; |
| 35 | + |
| 36 | + public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context) |
| 37 | + { |
| 38 | + var root = await context.Document.GetSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false); |
| 39 | + |
| 40 | + if (root is null) |
| 41 | + { |
| 42 | + return; |
| 43 | + } |
| 44 | + |
| 45 | + var diagnostic = context.Diagnostics.First(); |
| 46 | + var diagnosticSpan = diagnostic.Location.SourceSpan; |
| 47 | + var node = root.FindNode(diagnosticSpan); |
| 48 | + if (node is null) |
| 49 | + { |
| 50 | + return; |
| 51 | + } |
| 52 | + |
| 53 | + if (diagnostic.Properties.ContainsKey(EpiPartialControllerAnalyzer.PartialView) && node is IdentifierNameSyntax identifierNameSyntax) |
| 54 | + { |
| 55 | + context.RegisterCodeFix( |
| 56 | + CodeAction.Create( |
| 57 | + Resources.EpiPartialControllerTitle, |
| 58 | + c => ReplacePartialViewMethodAsync(context.Document, identifierNameSyntax, c), |
| 59 | + nameof(Resources.EpiPartialControllerTitle)), |
| 60 | + diagnostic); |
| 61 | + } |
| 62 | + else if (node is MethodDeclarationSyntax methodDeclaration) |
| 63 | + { |
| 64 | + |
| 65 | + context.RegisterCodeFix( |
| 66 | + CodeAction.Create( |
| 67 | + Resources.EpiPartialControllerTitle, |
| 68 | + c => ReplaceNameAndReturnParameterAsync(context.Document, methodDeclaration, c), |
| 69 | + nameof(Resources.EpiPartialControllerTitle)), |
| 70 | + diagnostic); |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + private static async Task<Document> ReplaceNameAndReturnParameterAsync(Document document, MethodDeclarationSyntax localDeclaration, CancellationToken cancellationToken) |
| 75 | + { |
| 76 | + var root = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false); |
| 77 | + |
| 78 | + var updatedName = localDeclaration.WithIdentifier(SyntaxFactory.Identifier("InvokeComponent")); |
| 79 | + var updatedReturnType = updatedName.WithReturnType(SyntaxFactory.ParseTypeName("IViewComponentResult")); |
| 80 | + var updatedAccessibility = updatedReturnType.WithModifiers(SyntaxFactory.TokenList(SyntaxFactory.Token(SyntaxKind.ProtectedKeyword), SyntaxFactory.Token(SyntaxKind.OverrideKeyword))); |
| 81 | + var newRoot = root!.ReplaceNode(localDeclaration, updatedAccessibility); |
| 82 | + |
| 83 | + // Return document with transformed tree. |
| 84 | + var updatedDocument = document.WithSyntaxRoot(newRoot); |
| 85 | + |
| 86 | + var compilationRoot = (await updatedDocument.GetSyntaxTreeAsync()).GetCompilationUnitRoot(); |
| 87 | + if (compilationRoot.Usings.Any(u => u.Name.ToString() == MvcNamespace)) |
| 88 | +{ |
| 89 | + var editor = await DocumentEditor.CreateAsync(updatedDocument, cancellationToken).ConfigureAwait(false); |
| 90 | + var documentRoot = (CompilationUnitSyntax)editor.OriginalRoot; |
| 91 | + var usingDirective = SyntaxFactory.UsingDirective(SyntaxFactory.ParseName(MvcNamespace).WithLeadingTrivia(SyntaxFactory.Whitespace(" "))) |
| 92 | + .WithTrailingTrivia(SyntaxFactory.CarriageReturnLineFeed); |
| 93 | + documentRoot = compilationRoot.AddUsings(usingDirective); |
| 94 | + editor.ReplaceNode(editor.OriginalRoot, documentRoot); |
| 95 | + updatedDocument = editor.GetChangedDocument(); |
| 96 | + } |
| 97 | + |
| 98 | + return updatedDocument; |
| 99 | + } |
| 100 | + |
| 101 | + private static async Task<Document> ReplacePartialViewMethodAsync(Document document, IdentifierNameSyntax identifierNameSyntax, CancellationToken cancellationToken) |
| 102 | + { |
| 103 | + var root = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false); |
| 104 | + |
| 105 | + var updatedName = identifierNameSyntax.WithIdentifier(SyntaxFactory.Identifier("View")); |
| 106 | + var newRoot = root!.ReplaceNode(identifierNameSyntax, updatedName); |
| 107 | + |
| 108 | + // Return document with transformed tree. |
| 109 | + return document.WithSyntaxRoot(newRoot); |
| 110 | + } |
| 111 | + } |
| 112 | +} |
0 commit comments