-
Notifications
You must be signed in to change notification settings - Fork 508
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1229 from sharwell/fix-1228
Implement a code fix and unit tests for SA1116
- Loading branch information
Showing
5 changed files
with
204 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
StyleCop.Analyzers/StyleCop.Analyzers/ReadabilityRules/ReadabilityResources.Designer.cs
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
86 changes: 86 additions & 0 deletions
86
StyleCop.Analyzers/StyleCop.Analyzers/ReadabilityRules/SA1116CodeFixProvider.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
namespace StyleCop.Analyzers.ReadabilityRules | ||
{ | ||
using System.Collections.Immutable; | ||
using System.Composition; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Helpers; | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.CodeActions; | ||
using Microsoft.CodeAnalysis.CodeFixes; | ||
using Microsoft.CodeAnalysis.CSharp; | ||
using Microsoft.CodeAnalysis.Text; | ||
|
||
/// <summary> | ||
/// Implements a code fix for <see cref="SA1116SplitParametersMustStartOnLineAfterDeclaration"/>. | ||
/// </summary> | ||
/// <remarks> | ||
/// <para>To fix a violation of this rule, ensure that the first parameter starts on the line after the opening | ||
/// bracket, or place all parameters on the same line if the parameters are not too long.</para> | ||
/// </remarks> | ||
[ExportCodeFixProvider(LanguageNames.CSharp, Name = nameof(SA1116CodeFixProvider))] | ||
[Shared] | ||
public class SA1116CodeFixProvider : CodeFixProvider | ||
{ | ||
/// <inheritdoc/> | ||
public override ImmutableArray<string> FixableDiagnosticIds { get; } = | ||
ImmutableArray.Create(SA1116SplitParametersMustStartOnLineAfterDeclaration.DiagnosticId); | ||
|
||
/// <inheritdoc/> | ||
public override FixAllProvider GetFixAllProvider() | ||
{ | ||
return CustomFixAllProviders.BatchFixer; | ||
} | ||
|
||
/// <inheritdoc/> | ||
public override Task RegisterCodeFixesAsync(CodeFixContext context) | ||
{ | ||
foreach (var diagnostic in context.Diagnostics) | ||
{ | ||
if (!diagnostic.Id.Equals(SA1116SplitParametersMustStartOnLineAfterDeclaration.DiagnosticId)) | ||
{ | ||
continue; | ||
} | ||
|
||
context.RegisterCodeFix( | ||
CodeAction.Create( | ||
ReadabilityResources.SA1116CodeFix, | ||
cancellationToken => GetTransformedDocumentAsync(context.Document, diagnostic, cancellationToken), | ||
equivalenceKey: nameof(SA1116CodeFixProvider)), | ||
diagnostic); | ||
} | ||
|
||
return SpecializedTasks.CompletedTask; | ||
} | ||
|
||
private static async Task<Document> GetTransformedDocumentAsync(Document document, Diagnostic diagnostic, CancellationToken cancellationToken) | ||
{ | ||
SyntaxNode root = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false); | ||
SyntaxToken originalToken = root.FindToken(diagnostic.Location.SourceSpan.Start); | ||
|
||
SyntaxTree tree = root.SyntaxTree; | ||
SourceText sourceText = await tree.GetTextAsync(cancellationToken).ConfigureAwait(false); | ||
TextLine sourceLine = sourceText.Lines.GetLineFromPosition(originalToken.SpanStart); | ||
|
||
string lineText = sourceText.ToString(sourceLine.Span); | ||
int indentLength; | ||
for (indentLength = 0; indentLength < lineText.Length; indentLength++) | ||
{ | ||
if (!char.IsWhiteSpace(lineText[indentLength])) | ||
{ | ||
break; | ||
} | ||
} | ||
|
||
IndentationOptions indentationOptions = IndentationOptions.FromDocument(document); | ||
SyntaxTriviaList newTrivia = | ||
SyntaxFactory.TriviaList( | ||
SyntaxFactory.CarriageReturnLineFeed, | ||
SyntaxFactory.Whitespace(lineText.Substring(0, indentLength) + IndentationHelper.GenerateIndentationString(indentationOptions, 1))); | ||
|
||
SyntaxToken updatedToken = originalToken.WithLeadingTrivia(originalToken.LeadingTrivia.AddRange(newTrivia)); | ||
SyntaxNode updatedRoot = root.ReplaceToken(originalToken, updatedToken); | ||
return document.WithSyntaxRoot(updatedRoot); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters