@@ -12,7 +12,6 @@ namespace StyleCop.Analyzers.SpacingRules
1212 using Microsoft . CodeAnalysis ;
1313 using Microsoft . CodeAnalysis . CodeActions ;
1414 using Microsoft . CodeAnalysis . CodeFixes ;
15- using Microsoft . CodeAnalysis . CSharp ;
1615 using Microsoft . CodeAnalysis . Text ;
1716 using StyleCop . Analyzers . Helpers ;
1817
@@ -49,53 +48,52 @@ public override Task RegisterCodeFixesAsync(CodeFixContext context)
4948 private static async Task < Document > GetTransformedDocumentAsync ( Document document , Diagnostic diagnostic , CancellationToken cancellationToken )
5049 {
5150 var indentationOptions = IndentationOptions . FromDocument ( document ) ;
52- var syntaxRoot = await document . GetSyntaxRootAsync ( cancellationToken ) . ConfigureAwait ( false ) ;
53-
54- var violatingTrivia = syntaxRoot . FindTrivia ( diagnostic . Location . SourceSpan . Start ) ;
55-
56- var stringBuilder = new StringBuilder ( ) ;
51+ SourceText sourceText = await document . GetTextAsync ( cancellationToken ) . ConfigureAwait ( false ) ;
52+ return document . WithText ( sourceText . WithChanges ( FixDiagnostic ( indentationOptions , sourceText , diagnostic ) ) ) ;
53+ }
5754
58- int firstTriviaIndex = violatingTrivia . GetLineSpan ( ) . StartLinePosition . Character ;
59- string relevantText ;
60- if ( firstTriviaIndex == 0 )
61- {
62- relevantText = violatingTrivia . ToFullString ( ) ;
63- }
64- else
65- {
66- SourceText sourceText = await document . GetTextAsync ( cancellationToken ) . ConfigureAwait ( false ) ;
67- relevantText = sourceText . ToString ( new TextSpan ( violatingTrivia . FullSpan . Start - firstTriviaIndex , firstTriviaIndex + violatingTrivia . FullSpan . Length ) ) ;
68- }
55+ private static TextChange FixDiagnostic ( IndentationOptions indentationOptions , SourceText sourceText , Diagnostic diagnostic )
56+ {
57+ TextSpan span = diagnostic . Location . SourceSpan ;
6958
59+ TextLine startLine = sourceText . Lines . GetLineFromPosition ( span . Start ) ;
60+ string text = sourceText . ToString ( TextSpan . FromBounds ( startLine . Start , span . End ) ) ;
61+ StringBuilder replacement = new StringBuilder ( ) ;
7062 int column = 0 ;
71- for ( int i = 0 ; i < relevantText . Length ; i ++ )
63+ for ( int i = 0 ; i < text . Length ; i ++ )
7264 {
73- char c = relevantText [ i ] ;
65+ char c = text [ i ] ;
7466 if ( c == '\t ' )
7567 {
7668 var offsetWithinTabColumn = column % indentationOptions . TabSize ;
7769 var spaceCount = indentationOptions . TabSize - offsetWithinTabColumn ;
7870
79- if ( i >= firstTriviaIndex )
71+ if ( i >= span . Start - startLine . Start )
8072 {
81- stringBuilder . Append ( ' ' , spaceCount ) ;
73+ replacement . Append ( ' ' , spaceCount ) ;
8274 }
8375
8476 column += spaceCount ;
8577 }
8678 else
8779 {
88- if ( i >= firstTriviaIndex )
80+ if ( i >= span . Start - startLine . Start )
8981 {
90- stringBuilder . Append ( c ) ;
82+ replacement . Append ( c ) ;
9183 }
9284
93- column ++ ;
85+ if ( c == '\n ' )
86+ {
87+ column = 0 ;
88+ }
89+ else
90+ {
91+ column ++ ;
92+ }
9493 }
9594 }
9695
97- var newSyntaxRoot = syntaxRoot . ReplaceTrivia ( violatingTrivia , SyntaxFactory . Whitespace ( stringBuilder . ToString ( ) ) ) ;
98- return document . WithSyntaxRoot ( newSyntaxRoot ) ;
96+ return new TextChange ( span , replacement . ToString ( ) ) ;
9997 }
10098 }
10199}
0 commit comments