33
44namespace StyleCop . Analyzers . ReadabilityRules
55{
6- using System . Collections . Generic ;
76 using System . Collections . Immutable ;
87 using System . Composition ;
9- using System . Linq ;
108 using System . Threading ;
119 using System . Threading . Tasks ;
1210 using Microsoft . CodeAnalysis ;
@@ -16,7 +14,6 @@ namespace StyleCop.Analyzers.ReadabilityRules
1614 using Microsoft . CodeAnalysis . CSharp . Syntax ;
1715 using Microsoft . CodeAnalysis . Text ;
1816 using StyleCop . Analyzers . Helpers ;
19- using StyleCop . Analyzers . SpacingRules ;
2017
2118 /// <summary>
2219 /// This class provides a code fix for <see cref="SA1106CodeMustNotContainEmptyStatements"/>.
@@ -25,11 +22,9 @@ namespace StyleCop.Analyzers.ReadabilityRules
2522 [ Shared ]
2623 internal class SA1106CodeFixProvider : CodeFixProvider
2724 {
28- private static readonly ImmutableArray < string > FixableDiagnostics =
29- ImmutableArray . Create ( SA1106CodeMustNotContainEmptyStatements . DiagnosticId ) ;
30-
3125 /// <inheritdoc/>
32- public override ImmutableArray < string > FixableDiagnosticIds => FixableDiagnostics ;
26+ public override ImmutableArray < string > FixableDiagnosticIds { get ; }
27+ = ImmutableArray . Create ( SA1106CodeMustNotContainEmptyStatements . DiagnosticId ) ;
3328
3429 /// <inheritdoc/>
3530 public override FixAllProvider GetFixAllProvider ( )
@@ -42,11 +37,6 @@ public override Task RegisterCodeFixesAsync(CodeFixContext context)
4237 {
4338 foreach ( var diagnostic in context . Diagnostics )
4439 {
45- if ( ! diagnostic . Id . Equals ( SA1106CodeMustNotContainEmptyStatements . DiagnosticId ) )
46- {
47- continue ;
48- }
49-
5040 context . RegisterCodeFix (
5141 CodeAction . Create (
5242 ReadabilityResources . SA1106CodeFix ,
@@ -62,10 +52,6 @@ private static async Task<Document> GetTransformedDocumentAsync(Document documen
6252 {
6353 var root = await document . GetSyntaxRootAsync ( cancellationToken ) . ConfigureAwait ( false ) ;
6454 var token = root . FindToken ( diagnostic . Location . SourceSpan . Start ) ;
65- if ( token . IsMissingOrDefault ( ) )
66- {
67- return document ;
68- }
6955
7056 if ( ! token . Parent . IsKind ( SyntaxKind . EmptyStatement ) )
7157 {
@@ -106,20 +92,39 @@ private static async Task<Document> RemoveEmptyStatementAsync(Document document,
10692
10793 private static async Task < Document > RemoveSemicolonTextAsync ( Document document , SyntaxToken token , CancellationToken cancellationToken )
10894 {
95+ TextChange textChange ;
96+
10997 SourceText sourceText = await document . GetTextAsync ( cancellationToken ) . ConfigureAwait ( false ) ;
11098 TextLine line = sourceText . Lines . GetLineFromPosition ( token . SpanStart ) ;
11199 if ( sourceText . ToString ( line . Span ) . Trim ( ) == token . Text )
112100 {
113101 // remove the line containing the semicolon token
114- TextChange textChange = new TextChange ( line . SpanIncludingLineBreak , string . Empty ) ;
102+ textChange = new TextChange ( line . SpanIncludingLineBreak , string . Empty ) ;
115103 return document . WithText ( sourceText . WithChanges ( textChange ) ) ;
116104 }
105+
106+ TextSpan spanToRemove ;
107+ var whitespaceIndex = TriviaHelper . IndexOfTrailingWhitespace ( token . LeadingTrivia ) ;
108+ if ( whitespaceIndex >= 0 )
109+ {
110+ spanToRemove = TextSpan . FromBounds ( token . LeadingTrivia [ whitespaceIndex ] . Span . Start , token . Span . End ) ;
111+ }
117112 else
118113 {
119- // remove just the semicolon
120- TextChange textChange = new TextChange ( token . Span , string . Empty ) ;
121- return document . WithText ( sourceText . WithChanges ( textChange ) ) ;
114+ var previousToken = token . GetPreviousToken ( ) ;
115+ whitespaceIndex = TriviaHelper . IndexOfTrailingWhitespace ( previousToken . TrailingTrivia ) ;
116+ if ( whitespaceIndex >= 0 )
117+ {
118+ spanToRemove = TextSpan . FromBounds ( previousToken . TrailingTrivia [ whitespaceIndex ] . Span . Start , token . Span . End ) ;
119+ }
120+ else
121+ {
122+ spanToRemove = token . Span ;
123+ }
122124 }
125+
126+ textChange = new TextChange ( spanToRemove , string . Empty ) ;
127+ return document . WithText ( sourceText . WithChanges ( textChange ) ) ;
123128 }
124129 }
125- }
130+ }
0 commit comments