-
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 #900 from vweijsters/SA1508
- Loading branch information
Showing
8 changed files
with
1,051 additions
and
28 deletions.
There are no files selected for viewing
846 changes: 846 additions & 0 deletions
846
StyleCop.Analyzers/StyleCop.Analyzers.Test/LayoutRules/SA1508UnitTests.cs
Large diffs are not rendered by default.
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
9 changes: 9 additions & 0 deletions
9
StyleCop.Analyzers/StyleCop.Analyzers/LayoutRules/LayoutResources.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
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
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
91 changes: 91 additions & 0 deletions
91
StyleCop.Analyzers/StyleCop.Analyzers/LayoutRules/SA1508CodeFixProvider.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,91 @@ | ||
namespace StyleCop.Analyzers.LayoutRules | ||
{ | ||
using System.Collections.Generic; | ||
using System.Collections.Immutable; | ||
using System.Composition; | ||
using System.Linq; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.CodeActions; | ||
using Microsoft.CodeAnalysis.CodeFixes; | ||
using Microsoft.CodeAnalysis.CSharp; | ||
|
||
/// <summary> | ||
/// Implements a code fix for <see cref="SA1508ClosingCurlyBracketsMustNotBePrecededByBlankLine"/>. | ||
/// </summary> | ||
[ExportCodeFixProvider(LanguageNames.CSharp, Name = nameof(SA1508CodeFixProvider))] | ||
[Shared] | ||
public class SA1508CodeFixProvider : CodeFixProvider | ||
{ | ||
private static readonly ImmutableArray<string> FixableDiagnostics = | ||
ImmutableArray.Create(SA1508ClosingCurlyBracketsMustNotBePrecededByBlankLine.DiagnosticId); | ||
|
||
/// <inheritdoc/> | ||
public override ImmutableArray<string> FixableDiagnosticIds => FixableDiagnostics; | ||
|
||
/// <inheritdoc/> | ||
public override FixAllProvider GetFixAllProvider() | ||
{ | ||
return WellKnownFixAllProviders.BatchFixer; | ||
} | ||
|
||
/// <inheritdoc/> | ||
public override Task RegisterCodeFixesAsync(CodeFixContext context) | ||
{ | ||
foreach (var diagnostic in context.Diagnostics.Where(d => FixableDiagnostics.Contains(d.Id))) | ||
{ | ||
context.RegisterCodeFix(CodeAction.Create(LayoutResources.SA1508CodeFix, token => GetTransformedDocumentAsync(context.Document, diagnostic, token)), diagnostic); | ||
} | ||
|
||
return Task.FromResult(true); | ||
} | ||
|
||
private static async Task<Document> GetTransformedDocumentAsync(Document document, Diagnostic diagnostic, CancellationToken cancellationToken) | ||
{ | ||
var syntaxRoot = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false); | ||
|
||
var closeBraceToken = syntaxRoot.FindToken(diagnostic.Location.SourceSpan.Start); | ||
var previousToken = closeBraceToken.GetPreviousToken(); | ||
|
||
var triviaList = previousToken.TrailingTrivia.AddRange(closeBraceToken.LeadingTrivia); | ||
|
||
// skip all leading whitespace for the close brace | ||
var index = triviaList.Count - 1; | ||
while (triviaList[index].IsKind(SyntaxKind.WhitespaceTrivia)) | ||
{ | ||
index--; | ||
} | ||
|
||
var firstLeadingWhitespace = index + 1; | ||
|
||
var done = false; | ||
var lastEndOfLineIndex = -1; | ||
while (!done && index >= 0) | ||
{ | ||
switch (triviaList[index].Kind()) | ||
{ | ||
case SyntaxKind.WhitespaceTrivia: | ||
break; | ||
case SyntaxKind.EndOfLineTrivia: | ||
lastEndOfLineIndex = index; | ||
break; | ||
default: | ||
done = true; | ||
break; | ||
} | ||
|
||
index--; | ||
} | ||
|
||
var replaceMap = new Dictionary<SyntaxToken, SyntaxToken>() | ||
{ | ||
[previousToken] = previousToken.WithTrailingTrivia(triviaList.Take(lastEndOfLineIndex + 1)), | ||
[closeBraceToken] = closeBraceToken.WithLeadingTrivia(triviaList.Skip(firstLeadingWhitespace)) | ||
}; | ||
|
||
var newSyntaxRoot = syntaxRoot.ReplaceTokens(replaceMap.Keys, (t1, t2) => replaceMap[t1]); | ||
return document.WithSyntaxRoot(newSyntaxRoot); | ||
} | ||
} | ||
} |
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