|
| 1 | +// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved. |
| 2 | +// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information. |
| 3 | + |
| 4 | +namespace StyleCop.Analyzers.DocumentationRules |
| 5 | +{ |
| 6 | + using System.Collections.Immutable; |
| 7 | + using System.Composition; |
| 8 | + using System.Linq; |
| 9 | + using System.Threading; |
| 10 | + using System.Threading.Tasks; |
| 11 | + using Helpers.ObjectPools; |
| 12 | + using Microsoft.CodeAnalysis; |
| 13 | + using Microsoft.CodeAnalysis.CodeActions; |
| 14 | + using Microsoft.CodeAnalysis.CodeFixes; |
| 15 | + using Microsoft.CodeAnalysis.CSharp; |
| 16 | + using Microsoft.CodeAnalysis.CSharp.Syntax; |
| 17 | + using StyleCop.Analyzers.Helpers; |
| 18 | + |
| 19 | + /// <summary> |
| 20 | + /// Implements the code fix for property summary documentation. |
| 21 | + /// </summary> |
| 22 | + [ExportCodeFixProvider(LanguageNames.CSharp, Name = nameof(PropertySummaryDocumentationCodeFixProvider))] |
| 23 | + [Shared] |
| 24 | + public class PropertySummaryDocumentationCodeFixProvider : CodeFixProvider |
| 25 | + { |
| 26 | + /// <inheritdoc/> |
| 27 | + public override ImmutableArray<string> FixableDiagnosticIds { get; } = |
| 28 | + ImmutableArray.Create( |
| 29 | + PropertySummaryDocumentationAnalyzer.SA1623Descriptor.Id, |
| 30 | + PropertySummaryDocumentationAnalyzer.SA1624Descriptor.Id); |
| 31 | + |
| 32 | + /// <inheritdoc/> |
| 33 | + public override FixAllProvider GetFixAllProvider() |
| 34 | + { |
| 35 | + return CustomFixAllProviders.BatchFixer; |
| 36 | + } |
| 37 | + |
| 38 | + /// <inheritdoc/> |
| 39 | + public override Task RegisterCodeFixesAsync(CodeFixContext context) |
| 40 | + { |
| 41 | + foreach (Diagnostic diagnostic in context.Diagnostics) |
| 42 | + { |
| 43 | + if (!diagnostic.Properties.ContainsKey(PropertySummaryDocumentationAnalyzer.NoCodeFixKey)) |
| 44 | + { |
| 45 | + context.RegisterCodeFix( |
| 46 | + CodeAction.Create( |
| 47 | + DocumentationResources.PropertySummaryStartTextCodeFix, |
| 48 | + cancellationToken => GetTransformedDocumentAsync(context.Document, diagnostic, cancellationToken), |
| 49 | + nameof(PropertySummaryDocumentationCodeFixProvider)), |
| 50 | + diagnostic); |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + return SpecializedTasks.CompletedTask; |
| 55 | + } |
| 56 | + |
| 57 | + private static async Task<Document> GetTransformedDocumentAsync(Document document, Diagnostic diagnostic, CancellationToken cancellationToken) |
| 58 | + { |
| 59 | + var syntaxRoot = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false); |
| 60 | + |
| 61 | + var node = syntaxRoot.FindNode(diagnostic.Location.SourceSpan); |
| 62 | + var documentation = node.GetDocumentationCommentTriviaSyntax(); |
| 63 | + |
| 64 | + var summaryElement = (XmlElementSyntax)documentation.Content.GetFirstXmlElement(XmlCommentHelper.SummaryXmlTag); |
| 65 | + var textElement = (XmlTextSyntax)summaryElement.Content.FirstOrDefault(); |
| 66 | + if (textElement == null) |
| 67 | + { |
| 68 | + return document; |
| 69 | + } |
| 70 | + |
| 71 | + var textToken = textElement.TextTokens.First(token => token.IsKind(SyntaxKind.XmlTextLiteralToken)); |
| 72 | + var text = textToken.ValueText; |
| 73 | + |
| 74 | + // preserve leading whitespace |
| 75 | + int index = 0; |
| 76 | + while (text.Length > index && char.IsWhiteSpace(text, index)) |
| 77 | + { |
| 78 | + index++; |
| 79 | + } |
| 80 | + |
| 81 | + var preservedWhitespace = text.Substring(0, index); |
| 82 | + |
| 83 | + // process the current documentation string |
| 84 | + string modifiedText; |
| 85 | + string textToRemove; |
| 86 | + if (diagnostic.Properties.TryGetValue(PropertySummaryDocumentationAnalyzer.TextToRemoveKey, out textToRemove)) |
| 87 | + { |
| 88 | + modifiedText = text.Substring(text.IndexOf(textToRemove) + textToRemove.Length).TrimStart(); |
| 89 | + } |
| 90 | + else |
| 91 | + { |
| 92 | + modifiedText = text.Substring(index); |
| 93 | + } |
| 94 | + |
| 95 | + if (modifiedText.Length > 0) |
| 96 | + { |
| 97 | + modifiedText = char.ToLowerInvariant(modifiedText[0]) + modifiedText.Substring(1); |
| 98 | + } |
| 99 | + |
| 100 | + // create the new text string |
| 101 | + var textToAdd = diagnostic.Properties[PropertySummaryDocumentationAnalyzer.ExpectedTextKey]; |
| 102 | + var newText = $"{preservedWhitespace}{textToAdd} {modifiedText}"; |
| 103 | + |
| 104 | + // replace the token |
| 105 | + var newXmlTextLiteral = SyntaxFactory.XmlTextLiteral(textToken.LeadingTrivia, newText, newText, textToken.TrailingTrivia); |
| 106 | + var newTextTokens = textElement.TextTokens.Replace(textToken, newXmlTextLiteral); |
| 107 | + var newTextElement = textElement.WithTextTokens(newTextTokens); |
| 108 | + |
| 109 | + var newSyntaxRoot = syntaxRoot.ReplaceNode(textElement, newTextElement); |
| 110 | + var newDocument = document.WithSyntaxRoot(newSyntaxRoot); |
| 111 | + |
| 112 | + return newDocument; |
| 113 | + } |
| 114 | + } |
| 115 | +} |
0 commit comments