|
| 1 | +// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved. |
| 2 | +// Licensed under the MIT license. See LICENSE in the project root for license information. |
| 3 | + |
| 4 | +namespace DocumentationAnalyzers.PortabilityRules |
| 5 | +{ |
| 6 | + using System; |
| 7 | + using System.Collections.Generic; |
| 8 | + using System.Collections.Immutable; |
| 9 | + using System.Composition; |
| 10 | + using System.Diagnostics; |
| 11 | + using System.Linq; |
| 12 | + using System.Threading; |
| 13 | + using System.Threading.Tasks; |
| 14 | + using System.Xml.Linq; |
| 15 | + using DocumentationAnalyzers.Helpers; |
| 16 | + using Microsoft.CodeAnalysis; |
| 17 | + using Microsoft.CodeAnalysis.CodeActions; |
| 18 | + using Microsoft.CodeAnalysis.CodeFixes; |
| 19 | + using Microsoft.CodeAnalysis.CSharp; |
| 20 | + using Microsoft.CodeAnalysis.CSharp.Syntax; |
| 21 | + |
| 22 | + [ExportCodeFixProvider(LanguageNames.CSharp, Name = nameof(DOC205CodeFixProvider))] |
| 23 | + [Shared] |
| 24 | + internal class DOC205CodeFixProvider : CodeFixProvider |
| 25 | + { |
| 26 | + public override ImmutableArray<string> FixableDiagnosticIds { get; } |
| 27 | + = ImmutableArray.Create(DOC205InheritDocumentation.DiagnosticId); |
| 28 | + |
| 29 | + public override FixAllProvider GetFixAllProvider() |
| 30 | + => CustomFixAllProviders.BatchFixer; |
| 31 | + |
| 32 | + public override Task RegisterCodeFixesAsync(CodeFixContext context) |
| 33 | + { |
| 34 | + foreach (var diagnostic in context.Diagnostics) |
| 35 | + { |
| 36 | + Debug.Assert(FixableDiagnosticIds.Contains(diagnostic.Id), "Assertion failed: FixableDiagnosticIds.Contains(diagnostic.Id)"); |
| 37 | + |
| 38 | + context.RegisterCodeFix( |
| 39 | + CodeAction.Create( |
| 40 | + PortabilityResources.DOC205CodeFix, |
| 41 | + token => GetTransformedDocumentAsync(context.Document, diagnostic, token), |
| 42 | + nameof(DOC205CodeFixProvider)), |
| 43 | + diagnostic); |
| 44 | + } |
| 45 | + |
| 46 | + return SpecializedTasks.CompletedTask; |
| 47 | + } |
| 48 | + |
| 49 | + private static async Task<Document> GetTransformedDocumentAsync(Document document, Diagnostic diagnostic, CancellationToken cancellationToken) |
| 50 | + { |
| 51 | + SyntaxNode root = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false); |
| 52 | + var xmlNode = (XmlNodeSyntax)root.FindNode(diagnostic.Location.SourceSpan, findInsideTrivia: true, getInnermostNodeForTie: true); |
| 53 | + var oldStartToken = xmlNode.GetName().LocalName; |
| 54 | + |
| 55 | + var semanticModel = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false); |
| 56 | + var documentedSymbol = semanticModel.GetDeclaredSymbol(xmlNode.FirstAncestorOrSelf<SyntaxNode>(SyntaxNodeExtensionsEx.IsSymbolDeclaration), cancellationToken); |
| 57 | + var candidateSymbol = GetCandidateSymbol(documentedSymbol); |
| 58 | + var candidateDocumentation = candidateSymbol.GetDocumentationCommentXml(expandIncludes: false, cancellationToken: cancellationToken); |
| 59 | + |
| 60 | + var xmlDocumentation = XElement.Parse(candidateDocumentation); |
| 61 | + var newLineText = Environment.NewLine; |
| 62 | + |
| 63 | + var content = new List<XmlNodeSyntax>(); |
| 64 | + content.AddRange(xmlDocumentation.Elements().Select(element => XmlSyntaxFactory.Node(newLineText, element))); |
| 65 | + |
| 66 | + var newStartToken = SyntaxFactory.Identifier(oldStartToken.LeadingTrivia, "autoinheritdoc", oldStartToken.TrailingTrivia); |
| 67 | + var newXmlNode = xmlNode.ReplaceToken(oldStartToken, newStartToken); |
| 68 | + |
| 69 | + if (newXmlNode is XmlElementSyntax newXmlElement) |
| 70 | + { |
| 71 | + var oldEndToken = newXmlElement.EndTag.Name.LocalName; |
| 72 | + var newEndToken = SyntaxFactory.Identifier(oldEndToken.LeadingTrivia, "autoinheritdoc", oldEndToken.TrailingTrivia); |
| 73 | + newXmlNode = newXmlNode.ReplaceToken(oldEndToken, newEndToken); |
| 74 | + } |
| 75 | + |
| 76 | + content.Add(XmlSyntaxFactory.NewLine(newLineText)); |
| 77 | + content.Add(newXmlNode); |
| 78 | + |
| 79 | + return document.WithSyntaxRoot(root.ReplaceNode(xmlNode, content)); |
| 80 | + } |
| 81 | + |
| 82 | + private static ISymbol GetCandidateSymbol(ISymbol memberSymbol) |
| 83 | + { |
| 84 | + if (memberSymbol is IMethodSymbol methodSymbol) |
| 85 | + { |
| 86 | + if (methodSymbol.MethodKind == MethodKind.Constructor || methodSymbol.MethodKind == MethodKind.StaticConstructor) |
| 87 | + { |
| 88 | + var baseType = memberSymbol.ContainingType.BaseType; |
| 89 | + return baseType.Constructors.Where(c => IsSameSignature(methodSymbol, c)).FirstOrDefault(); |
| 90 | + } |
| 91 | + else if (!methodSymbol.ExplicitInterfaceImplementations.IsEmpty) |
| 92 | + { |
| 93 | + // prototype(inheritdoc): do we need 'OrDefault'? |
| 94 | + return methodSymbol.ExplicitInterfaceImplementations.FirstOrDefault(); |
| 95 | + } |
| 96 | + else if (methodSymbol.IsOverride) |
| 97 | + { |
| 98 | + return methodSymbol.OverriddenMethod; |
| 99 | + } |
| 100 | + else |
| 101 | + { |
| 102 | + // prototype(inheritdoc): check for implicit interface |
| 103 | + return null; |
| 104 | + } |
| 105 | + } |
| 106 | + else if (memberSymbol is INamedTypeSymbol typeSymbol) |
| 107 | + { |
| 108 | + if (typeSymbol.TypeKind == TypeKind.Class) |
| 109 | + { |
| 110 | + // prototype(inheritdoc): when does base class take precedence over interface? |
| 111 | + return typeSymbol.BaseType; |
| 112 | + } |
| 113 | + else if (typeSymbol.TypeKind == TypeKind.Interface) |
| 114 | + { |
| 115 | + return typeSymbol.Interfaces.FirstOrDefault(); |
| 116 | + } |
| 117 | + else |
| 118 | + { |
| 119 | + // This includes structs, enums, and delegates as mentioned in the inheritdoc spec |
| 120 | + return null; |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + return null; |
| 125 | + } |
| 126 | + |
| 127 | + private static bool IsSameSignature(IMethodSymbol left, IMethodSymbol right) |
| 128 | + { |
| 129 | + if (left.Parameters.Length != right.Parameters.Length) |
| 130 | + { |
| 131 | + return false; |
| 132 | + } |
| 133 | + |
| 134 | + if (left.IsStatic != right.IsStatic) |
| 135 | + { |
| 136 | + return false; |
| 137 | + } |
| 138 | + |
| 139 | + if (!left.ReturnType.Equals(right.ReturnType)) |
| 140 | + { |
| 141 | + return false; |
| 142 | + } |
| 143 | + |
| 144 | + for (int i = 0; i < left.Parameters.Length; i++) |
| 145 | + { |
| 146 | + if (!left.Parameters[i].Type.Equals(right.Parameters[i].Type)) |
| 147 | + { |
| 148 | + return false; |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | + return true; |
| 153 | + } |
| 154 | + } |
| 155 | +} |
0 commit comments