From 6c620812a2544bf337de4bc347adc7d8db82076e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Hellander?= Date: Wed, 3 Jan 2024 17:47:00 +0100 Subject: [PATCH 1/2] Update to use a custom fixer for SA1629 #2957 --- .../SA1629CodeFixProvider.cs | 43 +++++++++++++++++-- 1 file changed, 40 insertions(+), 3 deletions(-) diff --git a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/DocumentationRules/SA1629CodeFixProvider.cs b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/DocumentationRules/SA1629CodeFixProvider.cs index 223cc203e..16f6d5904 100644 --- a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/DocumentationRules/SA1629CodeFixProvider.cs +++ b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/DocumentationRules/SA1629CodeFixProvider.cs @@ -5,6 +5,7 @@ namespace StyleCop.Analyzers.DocumentationRules { + using System.Collections.Generic; using System.Collections.Immutable; using System.Composition; using System.Threading; @@ -29,7 +30,7 @@ internal class SA1629CodeFixProvider : CodeFixProvider /// public override FixAllProvider GetFixAllProvider() { - return CustomFixAllProviders.BatchFixer; + return FixAll.Instance; } /// @@ -54,10 +55,46 @@ public override Task RegisterCodeFixesAsync(CodeFixContext context) private static async Task GetTransformedDocumentAsync(Document document, Diagnostic diagnostic, CancellationToken cancellationToken) { var text = await document.GetTextAsync(cancellationToken).ConfigureAwait(false); - bool replaceChar = diagnostic.Properties.ContainsKey(SA1629DocumentationTextMustEndWithAPeriod.ReplaceCharKey); - var newText = text.WithChanges(new TextChange(new TextSpan(diagnostic.Location.SourceSpan.Start, replaceChar ? 1 : 0), ".")); + var textChange = GetTextChange(diagnostic); + var newText = text.WithChanges(textChange); return document.WithText(newText); } + + private static TextChange GetTextChange(Diagnostic diagnostic) + { + var replaceChar = diagnostic.Properties.ContainsKey(SA1629DocumentationTextMustEndWithAPeriod.ReplaceCharKey); + var textChange = new TextChange(new TextSpan(diagnostic.Location.SourceSpan.Start, replaceChar ? 1 : 0), "."); + return textChange; + } + + private class FixAll : DocumentBasedFixAllProvider + { + public static FixAllProvider Instance { get; } = + new FixAll(); + + protected override string CodeActionTitle => + DocumentationResources.SA1629CodeFix; + + protected override async Task FixAllInDocumentAsync(FixAllContext fixAllContext, Document document, ImmutableArray diagnostics) + { + if (diagnostics.IsEmpty) + { + return null; + } + + var changes = new List(); + foreach (var diagnostic in diagnostics) + { + changes.Add(GetTextChange(diagnostic)); + } + + changes.Sort((left, right) => left.Span.Start.CompareTo(right.Span.Start)); + + var text = await document.GetTextAsync().ConfigureAwait(false); + var tree = await document.GetSyntaxTreeAsync().ConfigureAwait(false); + return await tree.WithChangedText(text.WithChanges(changes)).GetRootAsync().ConfigureAwait(false); + } + } } } From 58e9d96c85617c8aa943c6b9840a8ca58a4ab637 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Hellander?= Date: Wed, 3 Jan 2024 21:23:59 +0100 Subject: [PATCH 2/2] Fix review comments #2957 --- .../DocumentationRules/SA1629CodeFixProvider.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/DocumentationRules/SA1629CodeFixProvider.cs b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/DocumentationRules/SA1629CodeFixProvider.cs index 16f6d5904..8da151c9b 100644 --- a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/DocumentationRules/SA1629CodeFixProvider.cs +++ b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/DocumentationRules/SA1629CodeFixProvider.cs @@ -91,9 +91,9 @@ protected override async Task FixAllInDocumentAsync(FixAllContext fi changes.Sort((left, right) => left.Span.Start.CompareTo(right.Span.Start)); - var text = await document.GetTextAsync().ConfigureAwait(false); - var tree = await document.GetSyntaxTreeAsync().ConfigureAwait(false); - return await tree.WithChangedText(text.WithChanges(changes)).GetRootAsync().ConfigureAwait(false); + var text = await document.GetTextAsync(fixAllContext.CancellationToken).ConfigureAwait(false); + var tree = await document.GetSyntaxTreeAsync(fixAllContext.CancellationToken).ConfigureAwait(false); + return await tree.WithChangedText(text.WithChanges(changes)).GetRootAsync(fixAllContext.CancellationToken).ConfigureAwait(false); } } }