|
| 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 = "EP0007 CodeFix Provider")] |
| 25 | + public class EpiDisplayChannelCodeFixProvider : 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(EpiDisplayChannelAnalyzer.DiagnosticId); |
| 30 | + private const string HttpNamespace = "Microsoft.AspNetCore.Http"; |
| 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 methodDeclaration = root.FindNode(diagnosticSpan) as MethodDeclarationSyntax; |
| 48 | + if (methodDeclaration is null) |
| 49 | + { |
| 50 | + return; |
| 51 | + } |
| 52 | + |
| 53 | + context.RegisterCodeFix( |
| 54 | + CodeAction.Create( |
| 55 | + Resources.EpiDisplayChannelTitle, |
| 56 | + c => ReplaceNameAndReturnParameterAsync(context.Document, methodDeclaration, c), |
| 57 | + nameof(Resources.EpiDisplayChannelTitle)), |
| 58 | + diagnostic); |
| 59 | + } |
| 60 | + |
| 61 | + private static async Task<Document> ReplaceNameAndReturnParameterAsync(Document document, MethodDeclarationSyntax localDeclaration, CancellationToken cancellationToken) |
| 62 | + { |
| 63 | + var root = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false); |
| 64 | + |
| 65 | + var updatedParameter = localDeclaration.ParameterList.Parameters[0].WithType(SyntaxFactory.ParseTypeName("HttpContext")).WithTrailingTrivia(); |
| 66 | + var updatedParameterList = localDeclaration.WithParameterList(SyntaxFactory.ParameterList(SyntaxFactory.SingletonSeparatedList<ParameterSyntax>(updatedParameter))); |
| 67 | + var newRoot = root!.ReplaceNode(localDeclaration, updatedParameterList); |
| 68 | + |
| 69 | + // Return document with transformed tree. |
| 70 | + var updatedDocument = document.WithSyntaxRoot(newRoot); |
| 71 | + |
| 72 | + var compilationRoot = (await updatedDocument.GetSyntaxTreeAsync()).GetCompilationUnitRoot(); |
| 73 | + if (!compilationRoot.Usings.Any(u => u.Name.ToString() == HttpNamespace)) |
| 74 | + { |
| 75 | + var editor = await DocumentEditor.CreateAsync(updatedDocument, cancellationToken).ConfigureAwait(false); |
| 76 | + var documentRoot = (CompilationUnitSyntax)editor.OriginalRoot; |
| 77 | + var usingDirective = SyntaxFactory.UsingDirective(SyntaxFactory.ParseName(HttpNamespace).WithLeadingTrivia(SyntaxFactory.Whitespace(" "))) |
| 78 | + .WithTrailingTrivia(SyntaxFactory.CarriageReturnLineFeed); |
| 79 | + documentRoot = compilationRoot.AddUsings(usingDirective); |
| 80 | + editor.ReplaceNode(editor.OriginalRoot, documentRoot); |
| 81 | + updatedDocument = editor.GetChangedDocument(); |
| 82 | + } |
| 83 | + |
| 84 | + return updatedDocument; |
| 85 | + } |
| 86 | + } |
| 87 | +} |
0 commit comments