|
| 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.Threading; |
| 9 | + using System.Threading.Tasks; |
| 10 | + using Microsoft.CodeAnalysis; |
| 11 | + using Microsoft.CodeAnalysis.CodeActions; |
| 12 | + using Microsoft.CodeAnalysis.CodeFixes; |
| 13 | + using Microsoft.CodeAnalysis.Text; |
| 14 | + using StyleCop.Analyzers.Helpers; |
| 15 | + |
| 16 | + /// <summary> |
| 17 | + /// Implements a code fix for <see cref="SA1629DocumentationTextMustEndWithAPeriod"/>. |
| 18 | + /// </summary> |
| 19 | + [ExportCodeFixProvider(LanguageNames.CSharp, Name = nameof(SA1629CodeFixProvider))] |
| 20 | + [Shared] |
| 21 | + internal class SA1629CodeFixProvider : CodeFixProvider |
| 22 | + { |
| 23 | + /// <inheritdoc/> |
| 24 | + public override ImmutableArray<string> FixableDiagnosticIds { get; } |
| 25 | + = ImmutableArray.Create(SA1629DocumentationTextMustEndWithAPeriod.DiagnosticId); |
| 26 | + |
| 27 | + /// <inheritdoc/> |
| 28 | + public override FixAllProvider GetFixAllProvider() |
| 29 | + { |
| 30 | + return CustomFixAllProviders.BatchFixer; |
| 31 | + } |
| 32 | + |
| 33 | + /// <inheritdoc/> |
| 34 | + public override Task RegisterCodeFixesAsync(CodeFixContext context) |
| 35 | + { |
| 36 | + foreach (Diagnostic diagnostic in context.Diagnostics) |
| 37 | + { |
| 38 | + if (!diagnostic.Properties.ContainsKey(SA1629DocumentationTextMustEndWithAPeriod.NoCodeFixKey)) |
| 39 | + { |
| 40 | + context.RegisterCodeFix( |
| 41 | + CodeAction.Create( |
| 42 | + DocumentationResources.SA1629CodeFix, |
| 43 | + cancellationToken => GetTransformedDocumentAsync(context.Document, diagnostic, cancellationToken), |
| 44 | + nameof(SA1629CodeFixProvider)), |
| 45 | + diagnostic); |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + return SpecializedTasks.CompletedTask; |
| 50 | + } |
| 51 | + |
| 52 | + private static async Task<Document> GetTransformedDocumentAsync(Document document, Diagnostic diagnostic, CancellationToken cancellationToken) |
| 53 | + { |
| 54 | + var text = await document.GetTextAsync(cancellationToken).ConfigureAwait(false); |
| 55 | + var newText = text.WithChanges(new TextChange(new TextSpan(diagnostic.Location.SourceSpan.Start, 0), ".")); |
| 56 | + |
| 57 | + return document.WithText(newText); |
| 58 | + } |
| 59 | + } |
| 60 | +} |
0 commit comments